C/C++教程

各种数据库的jdbc驱动下载及连接方式(转载)

本文主要是介绍各种数据库的jdbc驱动下载及连接方式(转载),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
各种数据库驱动
数据库名称 下载地址 说明
Mysql http://www.mysql.com/products/connector/j/ Shipped. But need to download the latest for MySQL 4.1 or higher.
Oracle

http://sourceforge.net/project/showfiles.php?group_id=33291

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:dbc:" + 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连接的方式,可以作为一个手册使用。

  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();
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() ;


  二、JDBC连接MySql方式  

复制代码
package com.test1;
import java.sql.*;

public class MySQL_test1 {
    
    public static void main(String[] args) {

        String DriverName="org.gjt.mm.mysql.Driver";
        String URL="jdbc:mysql://127.0.0.1:3306/test";
        String UserName="wuyun2";
        String UserPassword="1234562";
    
        Connection ct=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        
        try {
            Class.forName(DriverName);
            ct=DriverManager.getConnection(URL, UserName, UserPassword);
            System.out.println("MySQL数据库连接成功!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
复制代码

 

二、JDBC连接Sql Server2000方式 

 

复制代码
 1 package com.mhl.db;
 2 import java.sql.*;
 3 
 4 public class SqlHelper {
 5 
 6     PreparedStatement ps=null;
 7     Connection ct=null;
 8     ResultSet rs=null;
 9     String sqlDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";    
10     String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=restaurant";
11     String user="wuyun";
12     String passwd="wuyun";
13     
14     public SqlHelper()
15     {
16         try {
17             Class.forName(sqlDriver);
18             ct=DriverManager.getConnection(url, user, passwd);
19             
20         } catch (Exception e) {
21             e.printStackTrace();
22         }        
23     }
复制代码
这篇关于各种数据库的jdbc驱动下载及连接方式(转载)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!