日期:2014-05-16  浏览次数:20600 次

jdbc连接五种数据库

package cn.com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBconn {

public static Connection connDB_mysql() throws ClassNotFoundException {// mysql

?? Class.forName("com.mysql.jdbc.Driver");

?? String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gbk";
?? String user = "root";
?? String password = "root";

?? Connection conn = null;

?? try {
??? conn = DriverManager.getConnection(url, user, password);
?? } catch (SQLException e) {
??? System.out.println("数据库链接异常!mysql");

??? e.printStackTrace();
?? }

?? return conn;
}

public static Connection connDB_sybase() throws ClassNotFoundException {// sybase
??????????????????? // ok

?? Class.forName("com.sybase.jdbc2.jdbc.SybDriver");

?? String url = "jdbc:sybase:Tds:192.168.14.238:5000/test&charset=cp936";
?? String user = "testlogin";
?? String password = "testlogin";

?? Connection conn = null;

?? try {
??? conn = DriverManager.getConnection(url, user, password);
?? } catch (SQLException e) {
??? System.out.println("数据库链接异常!sybase");

??? e.printStackTrace();
?? }

?? return conn;
}

public static Connection connDB_sqlserver() throws ClassNotFoundException {// sqlserver

?? Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

?? String url = "jdbc:microsoft:sqlserver://192.168.0.247:1433;DatabaseName=test";
?? String user = "sa";
?? String password = "";

?? Connection conn = null;

?? try {
??? conn = DriverManager.getConnection(url,user,password);
?? } catch (SQLException e) {
??? System.out.println("数据库链接异常!sqlserver");

??? e.printStackTrace();
?? }

?? return conn;
}

public static Connection connDB_oracle() throws ClassNotFoundException {// oracle ok

?? Class.forName("oracle.jdbc.driver.OracleDriver");

?? String url = "jdbc:oracle:thin:@192.168.0.246:1521:orcl";
?? String user = "test";
?? String password = "test";

?? Connection conn = null;

?? try {
??? conn = DriverManager.getConnection(url, user, password);
?? } catch (SQLException e) {
??? System.out.println("数据库链接异常!oracle");

??? e.printStackTrace();
?? }

?? return conn;
}

public static Connection connDB_db2() throws ClassNotFoundException {// db2

?? Class.forName("com.ibm.db2.jcc.DB2Driver");

?? String url = "jdbc:db2://192.168.0.2:50000/test:retrieveMessagesFromServerOnGetMessage=true;";
?? String user = "test";
?? String password = "test";

?? Connection conn = null;

?? try {
??? conn = DriverManager.getConnection(url, user, password);
?? } catch (SQLException e) {
??? System.out.println("数据库链接异常!db2");

??? e.printStackTrace();
?? }

?? return conn; }

?