`

Get Available Sql Types In A Database

    博客分类:
  • jdbc
 
阅读更多
转自:http://www.java2s.com/Code/Java/Database-SQL-JDBC/GetAvailableSqlTypesInADatabase.htm

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

  public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    ResultSet rs = null;
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTypeInfo();
    while (rs.next()) {
      // Get the database-specific type name
      String typeName = rs.getString("TYPE_NAME");

      // Get the java.sql.Types type to which this
      // database-specific type is mapped
      short dataType = rs.getShort("DATA_TYPE");

      // Get the name of the java.sql.Types value.
      System.out.println("type name="+typeName);
      System.out.println("dataType="+dataType);
      System.out.println("jdbcType="+dataType);
    }   
    st.close();
    conn.close();
  }

  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }

  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";

    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

}

分享到:
评论

相关推荐

    oracle-pl-sql-programming-5th-edition

    This book is the definitive reference on PL/SQL, considered throughout the database community to be the best Oracle programming book available. Like its predecessors, this fifth edition of Oracle PL/...

    Absolute Database for D7

    Overview Absolute Database: Delphi database with SQL support.Absolute Database lets you forget the Borland Database Engine (BDE). This BDE replacement is the compact, high-speed, robust and easy-to-...

    Oracle sqldeveloper without jdk (win+linux)

    - Bug 4918586: National character set datatypes (NCHAR, NVARCHAR2, NCLOB) are converted to the database character set datatypes (CHAR, VARCHAR2, CLOB) in the Run PL/SQL dialog box. Workaround: ...

    微软内部资料-SQL性能优化3

    An intent lock indicates that SQL Server wants to acquire a shared (S) lock or exclusive (X) lock on some of the resources lower down in the hierarchy. For example, a shared intent lock placed at the ...

    微软内部资料-SQL性能优化5

    In a nonclustered index, the leaf level contains each index key, plus a bookmark that tells SQL Server where to find the data row corresponding to the key in the index. A bookmark can take one of two ...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    From there, the examples mirror the sqlite3 examples, including creating a database and table, loading data in a CSV input file into a database table, updat‐ ing records in a table using a CSV input...

    DbfDotNet_version_1.0_Source

    When you don't have a SQL Server available When you want your footprint as small as possible and can't afford SQL Express When you want to manipulate or cache SQL data When you need to write highly...

    Sql for mysql

    CHAPTER 4 SQL in a Nutshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.2 Logging ...

    CE中文版-启点CE过NP中文.exe

    structure data from PDB files can now be used, and are stored in a database for lookup later dissect structures form can now show a list of known structures (pdb, mono, ...) Added a "revert to saved ...

    Orchard CMS Up and Running

    NHibernate is used in Orchard CMS for data access, allowing developers to work with databases using a higher-level, object-oriented API rather than SQL queries. This abstraction layer makes it easier...

    FastReport.v4.9.81 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版

    A built-in dialog designer to request parameters before running a report, plus a macro-language interpreter (four languages available - C++Script, PascalScript, BasicScript, JScript) that handles non-...

    mysql,sqlserver,oracle三种数据库的大对象存取解析.docx

    ps.setBinaryStream(2, in, in.available()); ps.executeUpdate(); ps.close(); // 读取操作 PreparedStatement psRead = conn.prepareStatement("SELECT * FROM tb_file WHERE filename = ?"); psRead....

    BobBuilder_app

    This means that you do a binary search in the page list in log M time and get the value in O(1) time within a page. RaptorDB starts off by loading the page list and it is good to go from there and...

    Borland Delphi 2005 Architect Update 3

    s should restrict master as well.0.00 of 5 Closed8417 TCustomSQLDataSet.GetFieldData implementation0.00 of 5 Closed8229 Error in code when database doesn't support schema/owner names5.00 of 5 ...

    ZendFramework中文文档

    9.5.2. Create a date from database 9.5.3. Create dates from an array 9.6. Constants for General Date Functions 9.6.1. Using Constants 9.6.2. List of All Constants 9.6.3. Self-Defined OUTPUT ...

    php.ini-development

    If you use constants in your value, and these constants belong to a ; dynamically loaded extension (either a PHP extension or a Zend extension), ; you may only use these constants *after* the line ...

    Doctrine ORM for PHP.pdf

    Lazy Database Connecting......................................................................................28 Testing your Connection...................................................................

Global site tag (gtag.js) - Google Analytics