常用数据库JDBC连接写法
2024-07-21 02:14:14
供稿:网友
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