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

数据库Oracle,mysql,sqlservice 连接
package com.myservlet;

import java.sql.Connection;

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

public class ConnectionGet {

private Connection con;
private String url;
private String serverName;
private String portNumber;
private String databaseName;
private String userName;
private String password;
private String sql;

ConnectionGet(){
url="jdbc:mysql://";
serverName="localhost";
portNumber="3306";
databaseName="testdb";
userName="root";
password="root";


}

private String getConnectionUrl(){
//*********************Oracle连接*************************
//return "jdbc:oracle:thin:@localhost:1521:ORCL";

//*******************sql service 2005 连接*********************
return "jdbc:sqlserver://localhost:1433;DatabaseName=test";
// return "jdbc:sqlserver://localhost:1433/test?user=sa&password=ADMIN"; 错误写法

//************mysql连接******************
// return url+serverName+":"+portNumber+"/"+databaseName+"?user="+userName+"&password="+password;
}
public  java.sql.Connection getConnection(){
try {

//Class.forName("oracle.jdbc.driver.OracleDriver");
//con=DriverManager.getConnection(getConnectionUrl(),"ZHICHENG","ADMIN");

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection(getConnectionUrl(),"sa","ADMIN");

// Class.forName("com.mysql.jdbc.Driver");
// con=DriverManager.getConnection(getConnectionUrl());

if(con!=null){
//System.out.println("连接成功");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return con;

}
}