Java JDBC

本文详细介绍了使用Java JDBC连接各种数据库的方法,包括JDBC-ODBC Bridge、Java DB、MySQL、Oracle、PostgreSQL、Microsoft SQL Server等,提供了相应的驱动名称、DB URL、用户名和密码的示例。
摘要由CSDN通过智能技术生成

1.JDBC-ODBC Bridge

Java Jar:None

Driver Name: sun.jdbc.odbc.JdbcOdbcDriver

DB URL:jdbc.odbc:<DB>

User Name:xxxx

Password:xxxx

2.Java DB(Network)(Derby)

Java Jar: derbyclient.jar

Driver Name:org.apache.derby.jdbc.ClientDriver

DB URL:jdbc:derby://<HOST>[:<PORT>]/databaseName[;attr1=value1[;...]] default port =1527

User Name:xxxx

Password:xxxx

3.Java DB(Embedded)(Derby)

Java Jar: derby.jar

Driver Name:org.apache.derby.jdbc.EmbeddedDriver

DB URL:jdbc:derby:<DB> (simlar as jdbcodbc)

User Name:xxxx

Password:xxxx

4.MySQL Connector/J(MySQL (Connector/J driver))

Java Jar: mysql-connector-java-3.1.11-bin.jar(在Linux上,MySQL的缺省用户为root,密码无)

Driver Name:com.mysql.jdbc.Driver(org.gjt.mm.mysql.Driver)

DB URL:jdbc:mysql://<hostname>[<:3306>]/<dbname>

User Name:xxxx

Password:xxxx

5.Oracle (OCI driver)

Java Jar: ojdbc14.jar

Driver Name:org.apache.derby.jdbc.EmbeddedDriver

DB URL:jdbc:oracle:oci8:@<database_name>

User Name:xxxx

Password:xxxx

6.Oracle (OCI driver)

Java Jar: classes12.jar

Driver Name:org.apache.derby.jdbc.EmbeddedDriver

DB URL:jdbc:oracle:thin:@<server>[:<1521>]:<database_name>

User Name:xxxx

Password:xxxx

7.PostgreSQL

Java Jar: postgresql-8.3dev-600.jdbc3.jar

Driver Name:org.apache.derby.jdbc.EmbeddedDriver

DB URL:jdbc:postgresql:[<//host>[:<5432>/]]<database>

User Name:xxxx

Password:xxxx

8.Microsoft SQL Server

Java Jar: classes12.jar

Driver Name:org.apache.derby.jdbc.EmbeddedDriver

DB URL:jdbc:microsoft:sqlserver://<server_name>:<1433>

User Name:xxxx

Password:xxxx

9.Microsoft SQL Server 2005

Java Jar: classes12.jar

Driver Name:org.apache.derby.jdbc.EmbeddedDriver

DB URL:jdbc:sqlserver://<server_name>:<port>[;databaseName=<dbname>]

User Name:xxxx

Password:xxxx

常用数据库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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值