您当前的位置:首页 > 数据库入门

JDBC连接MySQL|SQLServer|Oracle等数据库的【正确方法】

时间:2012-09-11  来源:零基础编程入门教程网|快速学会网站建设【asp.net容易】  作者:Lynn  点击:

摘要:数据库有db2,ms sql server,mysql,oracle,sysbase等,编程语言有asp.net,java,asp,vb,VC++等,每种应用程序基本上都会用到数据库进行数据存储,所以免不了要使用编程语言对数据库进行操作,今特此整理一份数据库连接字符串,各类数据库JDBC连接的正确方法,以代参考。

支持3lshow.com——asp.net入门教程,asp.net视频教程,asp.net实例教程【asp.net教程网】

    数据库有db2,ms sql server,mysql,oracle,sysbase等,编程语言有asp.net,java,asp,vb,VC++等,每种应用程序基本上都会用到数据库进行数据存储,所以免不了要使用编程语言对数据库进行操作,今特此整理一份数据库连接字符串,各类数据库JDBC连接的正确方法,以代参考。

    今天我将为大家介绍JDBC连接MySQL|SQLServer|Oracle等各大数据库的【正确方法】,帮助大家编写正确的数据库连接字符串

    具体方法有十多种数据库的连接方法,请大家选择合适的方法使用:

     方法一: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 ); 

     方法二: PostgreSQL(http://www.de.postgresql.org)pgjdbc2.jar 
        Class.forName( "org.postgresql.Driver" );
        cn = DriverManager.getConnection( "jdbc:postgresql://MyDbComputerNameOrIP/myDatabaseName", sUsr, sPwd );

    方法三: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 ); 

    方法四: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") 

    方法五:Microsoft SQLServer(http://jtds.sourceforge.net
        Class.forName( "net.sourceforge.jtds.jdbc.Driver" );
        cn = DriverManager.getConnection( "jdbc:jtds:sqlserver://MyDbComputerNameOrIP:1433/master", sUsr, sPwd ); 

     方法六: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 ); 

    方法七:ODBC 
        Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
        Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd ); 

    方法八: 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 ); 

    方法九:Microsoft SQL Server series (6.5, 7.x and 2000) and Sybase 10
    commet
        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"); 
     

    方法十: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");

    方法十一:IBM AS400主机在用的JDBC语法 
        有装V4R4以上版本的Client Access Express
        可以在C:Program FilesIBMClient Accessjt400lib
        找到 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"); 

    方法十二: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");

    方法十三: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)

    方法十四: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" );

    方法十五: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


     

上一篇:DB2客户端的[IBM][CLI Driver] SQL30081N【错误解决方案】    下一篇:安装ms SQL Server 2000 sp3 MSDE过程系统提示指定的实例名称无效