日期:2014-05-16  浏览次数:20540 次

常用数据库JDBC连接写法

1.MySQL

Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql://IPAddr:3306/myDatabaseName?useUnicode=true&characterEncoding=utf8", "user", "password");

2.Oracle

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

con = DriverManager.getConnection("jdbc:oracle:thin:@IPAddr:1521:myDatabaseName");

3.DB2 
Class.forName("com.ibm.db2.jdbc.net.DB2Driver"); 
String url="jdbc:db2://IPAddr:6789/myDatabaseName" 
con = DriverManager.getConnection( url, user, password );

4. PostgreSQL(http://www.de.postgresql.org)pgjdbc2.jar 
Class.forName( "org.postgresql.Driver" ); 
cn = DriverManager.getConnection( "jdbc:postgresql://MyDbComputerNameOrIP/myDatabaseName", sUsr, sPwd ); 
5. Sybase(http://jtds.sourceforge.net)jconn2.jar 
Class*forJame( "com.sybase.jdbc2.jdbc.SybDriver" ); 
cn = DriverManager.getConnection( "jdbc:sybase:Tds:MyDbComputerNameOrIP:2638", sUsr, sPwd ); 
//(Default-Username/Password: "dba"/"sql") 
6. Microsoft SQLServer(http://jtds.sourceforge.net) 
Class.forName( "net.sourceforge.jtds.jdbc.Driver" ); 
cn = DriverManager.getConnection( "jdbc:jtds:sqlserver://MyDbComputerNameOrIP:1433/master", sUsr, sPwd ); 
7. 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 ); 
8. ODBC 
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); 
Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );