各種資料庫的JDBC驅動下載及連線字串URL寫法

掃地一小僧發表於2007-11-01
 各種資料庫的JDBC驅動下載及連線字串URL寫法

sun官方網站上的JDBC驅動列表:http://java.sun.com/products/jdbc/reference/industrysupport/index.html


數 據 庫  說      明 
MySQL  http://www.mysql.com/products/connector/j/ Shipped. But need to download the latest for MySQL 4.1 or higher. 
Oracle  http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html Included. 
SQL Server by jTDS  http://sourceforge.net/project/showfiles.php?group_id=33291 Included. Support Microsoft SQL Server (6.5, 7, 2000 and 2005) 
Postgres  http://jdbc.postgresql.org/download.html Included 7.3 JDBC 3 
SAP DB  http://www.sapdb.org/sap_db_jdbc.htm Included. 
SyBase by jTDS  http://jtds.sourceforge.net/ Included. Support Sybase (10, 11, 12)

以下內容來自網際網路

1. MySQL(http://www.mysql.com) mysql-connector-java-2.0.14-bin.jar ;
  Class.forName( "org.gjt.mm.mysql.Driver" );
  cn = DriverManager.getConnection( "jdbc:mysql://MyDbComputerNameOrIP:3306/myDatabaseName", sUsr, sPwd );
2. PostgreSQL(http://www.de.postgresql.org) pgjdbc2.jar ;
  Class.forName( "org.postgresql.Driver" );
  cn = DriverManager.getConnection( "jdbc:postgresql://MyDbComputerNameOrIP/myDatabaseName", sUsr, sPwd );
3. Oracle(http://www.oracle.com/ip/deploy/database/oracle9i/) classes12.zip ;
  Class.forName( "oracle.jdbc.driver.OracleDriver" );
  cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
4. Sybase(http://jtds.sourceforge.net) jconn2.jar ;
  Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
  cn = DriverManager.getConnection( "jdbc:sybase:Tds:MyDbComputerNameOrIP:2638", sUsr, sPwd );
  //(Default-Username/Password: "dba"/"sql")
5. Microsoft SQLServer(http://jtds.sourceforge.net) ;
  Class.forName( "net.sourceforge.jtds.jdbc.Driver" );
  cn = DriverManager.getConnection( "jdbc:jtds:sqlserver://MyDbComputerNameOrIP:1433/master", sUsr, sPwd );
6. Microsoft SQLServer(http://www.microsoft.com) ;
  Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
  cn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://MyDbComputerNameOrIP:1433;databaseName=master", sUsr, sPwd );
7. ODBC
  Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
  Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );
8.DB2 Class.forName("com.ibm.db2.jdbc.net.DB2Driver");
  String url="jdbc:db2://192.9.200.108:6789/SAMPLE"
  cn = DriverManager.getConnection( url, sUsr, sPwd );
9.access由於access並不是作為一項服務執行,所以url的方法對他不適用。access可以通過odbc,也可以通過伺服器對映路徑的形式找到.mdb檔案,參見http://rmijdbc.objectweb.org/Access/access.html

JDBC API的使用方法
(1)登記並載入JDBC驅動程式;
兩種方法:
Class.forName(String drivername);
DriverManager.registerDriver(Driver driver)
(2)建立與SQL資料庫的連線;
DriverManager的getConnection()方法:
Connection getConnection(String url):url表示資料庫地址字串;
Connection getConnection(String url,String user,String pwd)
Connection getConnection(String url,Properties info)
(3)傳送一個SQL查詢;
Connection的createStatement()方法:
Statement createStatement();
Statement可以執行SQL語句,得到SQL查詢結果。
(4)獲得結果集。
Statement的執行SQL語句方法:
ResultSet executeQuery(String sql):執行select語句
int executeUpdate(String sql):執行更新語句,如insert,delete,update.
(5)檢索查詢結果。
ResultSet的方法:
boolean next():沒有行時返回false;
String getString(String columnName):返回列名對應的值。
posted on 2007-07-01 16:09 和田雨 閱讀(153) 評論(1)  編輯 收藏 引用 所屬分類: JDBC  


為了方便大家查詢,整理內容如下:
1. MySQL(http://www.mysql.com)mm.mysql-2.0.2-bin.jar
Class.forName( "org.gjt.mm.mysql.Driver" );
cn = DriverManager.getConnection( "jdbc:mysql://MyDbComputerNameOrIP:3306/myDatabaseName", sUsr, sPwd );

2. PostgreSQL(http://www.de.postgresql.org)pgjdbc2.jar
Class.forName( "org.postgresql.Driver" );
cn = DriverManager.getConnection( "jdbc:postgresql://MyDbComputerNameOrIP/myDatabaseName", sUsr, sPwd );

3. Oracle(http://www.oracle.com/ip/deploy/database/oracle9i/)classes12.zip
Class.forName( "oracle.jdbc.driver.OracleDriver" );
cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );

4. Sybase(http://jtds.sourceforge.net)jconn2.jar
Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
cn = DriverManager.getConnection( "jdbc:sybase:Tds:MyDbComputerNameOrIP:2638", sUsr, sPwd );
//(Default-Username/Password: "dba"/"sql")

5. Microsoft SQLServer(http://jtds.sourceforge.net)
Class.forName( "net.sourceforge.jtds.jdbc.Driver" );
cn = DriverManager.getConnection( "jdbc:jtds:sqlserver://MyDbComputerNameOrIP:1433/master", sUsr, sPwd );

6. Microsoft SQLServer(http://www.microsoft.com)
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
cn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://MyDbComputerNameOrIP:1433;databaseName=master", sUsr, sPwd );

7. ODBC
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );

8.DB2(新新增)
Class.forName("com.ibm.db2.jdbc.net.DB2Driver");
String url="jdbc:db2://192.9.200.108:6789/SAMPLE"
cn = DriverManager.getConnection( url, sUsr, sPwd );

9.Microsoft SQL Server series (6.5, 7.x and 2000) and Sybase 10

JDBC Name: jTDS
URL: http://jtds.sourceforge.net/
Version: 0.5.1
Download URL: http://sourceforge.net/project/showfiles.php?group_id=33291

語法:
Class.forName("net.sourceforge.jtds.jdbc.Driver ");
Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://host:port/database","user","password");
or
Connection con = DriverManager.getConnection("jdbc:jtds:sybase://host:port/database","user","password");

10.Postgresql
JDBC Name: PostgreSQL JDBC
URL: http://jdbc.postgresql.org/
Version: 7.3.3 build 110
Download URL: http://jdbc.postgresql.org/download.html
語法:
Class.forName("org.postgresql.Driver");
Connection con=DriverManager.getConnection("jdbc:postgresql://host:port/database","user","password");

11.IBM AS400主機在用的JDBC語法
有裝V4R4以上版本的Client Access Express
可以在C:/Program Files/IBM/Client Access/jt400/lib
找到 driver 檔案 jt400.zip,並更改副檔名成為 jt400.jar
語法:
java.sql.DriverManager.registerDriver (new com.ibm.as400.access.AS400JDBCDriver ());
Class.forName("com.ibm.as400.access.AS400JDBCConnection");
con = DriverManager.getConnection("jdbc:as400://IP","user","password");

12.informix
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/testDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
Lib:jdbcdrv.zip<br><br>Class.forName( "com.sybase.jdbc.SybDriver" )
url="jdbc:sybase:Tds:127.0.0.1:2638/asademo";
SybConnection connection= (SybConnection)DriverManager.getConnection(url,"dba","sql");

13.SAP DB
Class.forName ("com.sap.dbtech.jdbc.DriverSapDB");
java.sql.Connection connection = java.sql.DriverManager.getConnection ( "jdbc:sapdb://" + host + "/" + database_name,user_name, password)

14.InterBase
String url = "jdbc:interbase://localhost/e:/testbed/database/employee.gdb";
Class.forName("interbase.interclient.Driver");
//Driver d = new interbase.interclient.Driver (); /* this will also work if you do not want the line above */
Connection conn = DriverManager.getConnection( url, "sysdba", "masterkey" );

15.HSqlDB
url: http://hsqldb.sourceforge.net/
driver: org.hsqldb.jdbcDriver
連線方式有4種,分別為:
con-str(記憶體): jdbc:hsqldb.
con-str(本地): jdbc:hsqldb:/path/to/the/db/dir
con-str(http): jdbc:hsqldb:http://dbsrv
con-str(hsql): jdbc:hsqldb:hsql://dbsrv

相關文章