论坛首页 入门技术论坛

各种数据库连接的驱动名与示例

浏览 2011 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-02-26   最后修改:2009-02-26
  switch (type) {
                case SQLSERVER:
                    String url = "jdbc:sqlserver://" + host + ":" + port + ";" + "dataBaseName=" + dbName;
//                    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();//method1(the way of load Driver
//                    DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());//method2
                    com.microsoft.jdbc.sqlserver.SQLServerDriver sql = new com.microsoft.jdbc.sqlserver.SQLServerDriver();//method3
                    con = (Connection) DriverManager.getConnection(url, userName, password);
                    break;
                case MYSQL:
                    String url1 = "jdbc:mysql://" + host + "/" + dbName + "?characterEncoding=utf-8&user=";
//                    String url1 = "jdbc:mysql://" + host + "?&useUnicode=true&characterEncoding=8859_1";
                    //following way is the first way to load driver
//                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    //the dirver is also could be the following
                    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
                    con = (Connection) DriverManager.getConnection(url1, userName, password);
                    break;
                case ORACLE:
                    String url2 = "jdbc:oracle:thin:@" + host + ":" + port + ":" + dbName;
                    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
                    con = DriverManager.getConnection(url2, userName, password);
                    break;
                case DB2:
                    Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
                    String url4 = "jdbc:db2://" + host + ":" + port + "/" + dbName; //sample为你的数据库名 
                    con = DriverManager.getConnection(url4, userName, password);
                    break;
                case SYBASE:
                    Class.forName("com.sybase.jdbc.SybDriver").newInstance();
                    String url5 = " jdbc:sybase:Tds:" + host + ":" + port + "/" + dbName;//myDB为你的数据库名 
                    Properties sysProps = System.getProperties();
                    sysProps.put("user", "userid");
                    sysProps.put("password", "user_password");
                    con = DriverManager.getConnection(url5, sysProps);
                    break;
                case SYSPROPS:
                    Class.forName("com.informix.jdbc.IfxDriver").newInstance();
                    String url6 = "jdbc:informix-sqli://" + host + ":" + port + "/" + dbName;
                    con = DriverManager.getConnection(url6, userName, password);
                    break;
                case POSTGRESQL:
                    Class.forName("org.postgresql.Driver").newInstance();
                    String url7 = "jdbc:postgresql://" + host + "/" + dbName; //myDB为数据库名 
                    con = DriverManager.getConnection(url7, userName, password);
                    break;
                case ACCESS:
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    String url8 = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=" + dbName;
                    con = DriverManager.getConnection(url8, userName, password);
                    break;
            }

 

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics