jdbc连接各种数据库

Database 专栏收录该内容
5 篇文章 0 订阅

Java数据库连接(JDBC)由一组用 Java 编程语言编写的类和接口组成。JDBC 为工具/数据库开发人员提供了一个标准的 API,使他们能够用纯Java API 来编写数据库应用程序。然而各个开发商的接口并不完全相同,所以开发环境的变化会带来一定的配置变化。本文主要集合了不同数据库的连接方式。

下面罗列了各种数据库使用JDBC连接的方式,可以作为一个手册使用。

1、Oracle8/8i/9i数据库(thin模式)

 

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
            String user="test";
            String password="test";
            Connection conn= DriverManager.getConnection(url,user,password);

 

 

 

  2、DB2数据库

 

Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
            String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名
            String user="admin";
            String password="";
            Connection conn= DriverManager.getConnection(url,user,password);

 

 

 

  3、Sql Server7.0/2000数据库

 

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
            String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
            //mydb为数据库
            String user="sa";
            String password="";
            Connection conn= DriverManager.getConnection(url,user,password);

 

 

 

  4、Sybase数据库

 

Class.forName("com.sybase.jdbc.SybDriver").newInstance();
            String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名
            Properties sysProps = System.getProperties();
            SysProps.put("user","userid");
            SysProps.put("password","user_password");
            Connection conn= DriverManager.getConnection(url, SysProps);

 

 

 

  5、Informix数据库

 

 

Class.forName("com.informix.jdbc.IfxDriver").newInstance();
            String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
            user=testuser;password=testpassword"; //myDB为数据库名
            Connection conn= DriverManager.getConnection(url);

 

 

  6、MySQL数据库

 

Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            //或者Class.forName("com.mysql.jdbc.Driver");
            String url ="jdbc:mysql://localhost/myDB?
            user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
            //myDB为数据库名
            Connection conn= DriverManager.getConnection(url);

 

 

 

  7、PostgreSQL数据库

 

Class.forName("org.postgresql.Driver").newInstance();
            String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名
            String user="myuser";
            String password="mypassword";
            Connection conn= DriverManager.getConnection(url,user,password);

 

 

 

  8、access数据库直连用ODBC的

 

 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
            String url="jdbc:odbc:Driver={MicroSoft Access Driver
            (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
            Connection conn = DriverManager.getConnection(url,"","");
            Statement stmtNew=conn.createStatement() ;

当MySQL集群时, JDBC路径的使用方法.这样使用:    jdbc:mysql://[host:port],[host:port].../[database][?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...例如:    jdbc:mysql://host1:3306,host2:3307,host3:3308/test?user=root&passWord=root&autoReconnect=true&failOverReadOnly=false注重:    我在使用的时候发现一个问题,当使用3.0.14版本的JDBC时,使用上述方法出现异常:        Java.sql.SQLException: Server connection failure during transaction.
       Attempted reconnect 3 times. Giving up.
       ... ...    在更新到3.0.15版本后问题没有出现,这可能是3.0.14这个版本的一个Bug。

 

各种数据库的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
 


评论
# re: 各种数据库的JDBC驱动下载及连接字符串URL写法 2007-07-02 19:28 和田雨
为了方便大家查找,整理内容如下:
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

 

为了自己JDBC驱动下载方便快捷,也便于网友的使用,特意把这些JDBC的驱动包下载汇总如下,。。。。说明:此内容来源于网络!。。。。。

Microsoft SQL Server (6.5, 7, 2000 and 2005) and Sybase (10, 11, 12).  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JDBC Name: jTDS
Home Page: http://jtds.sourceforge.net/
JDBC Ver: 1.0.2
Download: http://sourceforge.net/project/showfiles.php?group_id=33291
Conn Code:
   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");

Microsoft SQL Server 2000
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JDBC Name: SQL Server 2000 Driver for JDBC
Home Page: http://www.microsoft.com/china/sql/
JDBC Ver: (SQL Server 2000 Driver for JDBC)
Download: http://www.microsoft.com/china/sql/downloads/2000/jdbc.asp
Conn Code:
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");  
   Connection conn = DriverManager.getConnection ("jdbc:microsoft:sqlserver://server1:1433","user","password");

Oracle
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JDBC Name: Connector/J  
Home Page: http://www.oracle.com/technology/software/tech/java/
JDBC Ver: (based on Oracle)
Download: http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/
Conn Code:
   Class.forName("oracle.jdbc.driver.OracleDriver");
   Connection con = DriverManager.getConnection("jdbc:oracle:thin:@host:port:databse","user","password");

MySQL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JDBC Name: Connector/J
Home Page: http://dev.mysql.com/
JDBC Ver: 3.1
Download: http://dev.mysql.com/downloads/
Conn Code:
   Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection("jdbc:mysql://host:port/database","user","password");

Sybase
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JDBC Name: jConnect  
Home Page: http://www.sybase.com.cn/cn/content/SDN/
JDBC Ver: 4.5/5.5
Download: http://www.sybase.com/detail_list?id=13&morenavId=8668&multi=true&SR=Y&show=1265
Conn Code:
Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();
DriverManager.getConnection("jdbc:sybase:Tds:IP:2638?ServiceName="+database,"user","password");

Postgresql
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JDBC Name: PostgreSQL JDBC
Home Page: http://jdbc.postgresql.org/
JDBC Ver: 8.0 Build 310
Download: http://jdbc.postgresql.org/download.html
Conn Code:
   Class.forName("org.postgresql.Driver");  
   Connection con = DriverManager.getConnection("jdbc:postgresql://host:port/database","user","password");

IBM Informix
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JDBC Name: Informix JDBC Driver (IBM Informix JDBC V3.0)
Home Page: http://www-306.ibm.com/software/data/informix/
JDBC Ver: 3.0.JC1
Download: http://www14.software.ibm.com/webapp/download/search.jsp?go=y&rs=ifxjdbc
Conn Code:
   Class.forName("com.informix.jdbc.IfxDriver").newInstance();  
   Connection conn= DriverManager.getConnection("jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";);

IBM DB2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JDBC Name: DB2 JDBC Universal Driver
Home Page: http://www-306.ibm.com/software/data/db2/udb/  
JDBC Ver:  
Download: http://www-128.ibm.com/developerworks/db2/downloads/jcc/
Conn Code:
   Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();  
   Connection conn= DriverManager.getConnection("jdbc:db2://localhost:5000/sample",user,password);  

IBM AS400主机在用的JDBC语法
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  有装V4R4以上版本的Client Access Express
  可以在C:"Program Files"IBM"Client Access"jt400"lib
  找到 driver 档案 jt400.zip,并更改扩展名成为 jt400.jar
Conn Code:
   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");  

   Db Name: Ms Access or Ms Foxpro
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Conn Code:
   step 1: 配置 odbc (例如:myDb)
   step 2:  
   String dbURL = "jdbc:odbc:myDb";
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 Connection conn = DriverManager.getConnection(dbURL);  
or
   String dbURL = "jdbc:odbc:driver={MicrosoftAccessDriver(*.mdb)};DBQ=dabaseName.mdb";
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 Connection conn = DriverManager.getConnection(dbURL);


  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

参与评论
请先登录 后发表评论~
©️2021 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值