Java中数据库驱动程序加载及建立连接

JeanV 2011-04-02

//Oracle数据库:

Class.forName("oracle.jdbc.driver.OracleDriver");

Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","username","password");

//注:orcl为数据库的SID

//DB2数据库:

Class.forName("com.ibm.db2.jdbc.app.DB2Driver");

Connectionconn=DriverManager.getConnection("jdbc:db2://localhost:5000/dbname","username","password");

//注:dbname为数据库名

//SQLServer数据库:

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

Connectionconn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1443;DatabaseName=dbaname","username","password");

//注:dbname为数据库名

//Sybase数据库:

   //Class.forName("com.sybase.jdbc.SybDriver");
Class.forName("com.sybase.jdbc2.jdbc.SybDriver");  
Connection conn = DriverManager.getConnection("jdbc:sybase:Tds:localhost:5007/dbname" , "username" , "password");

注:dbname为数据库名

//Informix数据库:

Class.forName("com.informix.jdbc.IfxDriver");

//MySQL数据库:

Class.forName("org.gjt.mm.mysql.Driver");或者Class.forName("com.mysql.jdbc.Driver");

Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","username","password");

   //注:dbname为数据库名

//Postgresql

Class.forName("org.postgresql.Driver");  

Connectioncon=DriverManager.getConnection("jdbc:postgresql://host:port/database","user","password");

相关推荐