/* * Db.java
Created on 2007年8月20日, 上午 8:37
*/
importjava.io.*;
importjava.sql.*;
importjava.util.Properties;
public class Db {
private String driver;
private String url;
private String user;
private String password;
private Connection conn;
private Statement stm;
private ResultSet rs;
public Db(){
this("DBConf.properties");
}
public Db(String conf) {
loadProperties(conf);
setConn();
}
public Connection getConn(){
return this.conn;
}
//handlethe properties file to get the informations for connection
private void loadProperties(Stringconf){
Properties props = newProperties();
try {
props.load(newFileInputStream(conf));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.driver = props.getProperty("driver");
this.url = props.getProperty("url");
this.user = props.getProperty("user");
this.password =props.getProperty("password");
}
//implementthe Connection
private void setConn(){
try {
Class.forName(driver);
this.conn =DriverManager.getConnection(url,user,password);
} catch(ClassNotFoundException classnotfoundexception){
classnotfoundexception.printStackTrace();
System.err.println("db:" + classnotfoundexception.getMessage());
} catch(SQLException sqlexception) {
System.err.println("db.getconn(): " +sqlexception.getMessage());
}
}
public void doInsert(String sql) {
try {
Statement statement =conn.createStatement();
int i =stm.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeInset:" +sqlexception.getMessage());
}
}
public void doDelete(String sql) {
try {
stm =conn.createStatement();
int i =stm.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeDelete:" +sqlexception.getMessage());
}
}
public void doUpdate(String sql) {
try {
stm =conn.createStatement();
int i =stm.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeUpdate:" +sqlexception.getMessage());
}
}
public ResultSet doSelect(String sql) {
try {
stm =conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs =stm.executeQuery(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeQuery: " +sqlexception.getMessage());
}
return rs;
}
public static void main(String[] args){
try{
Db db = new Db();
Connection conn =db.getConn();
if(conn != null &&!conn.isClosed()) {
System.out.println("連結成功");
ResultSet rs =db.doSelect("select * from content");
while(rs.next()){
System.out.println(rs.getString(1)+":"+rs.getString(2)+":"+rs.getString(3));
}
rs.close();
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
DBConf.properties:
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@tdt151:1521:train
user=XX
password=XX
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
public classConnectDB {
private static finalString MYSQL = "jdbc:mysql://";
private static finalString ORACLE = "jdbc:oracle:thin:@";
private ConnectDB() {
}
public staticConnection getInstance(String DBType, String url)
throwsNoSuchDBException, SQLException {
if("mysql".equalsIgnoreCase(DBType))
returngetMySqlConn(url);
if("oracle".equalsIgnoreCase(DBType))
returngetOracleConn(url);
return null;
}
public static voidcloseConn(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLExceptione) {
e.printStackTrace();
}
}
}
private staticConnection getMySqlConn(String url) throws SQLException {
Connection conn =null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
conn =DriverManager.getConnection(MYSQL + url, "root", "root");
return conn;
}
private staticConnection getOracleConn(String url) throws SQLException {
Connection conn =null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
conn =DriverManager.getConnection(ORACLE + url, "scott","tiger");
return conn;
}
}
public static StringgetURLByDBInfo(DBInfo dbInfo)
{
String url ="";
if(dbInfo.getDbType() != null)
if(dbInfo.getDbType().equals("SQLSERVER"))
url = (newStringBuilder("jdbc:microsoft:sqlserver://")).append(dbInfo.getDbHost()).append(":").append(dbInfo.getDbPort()).append(";DatabaseName=").append(dbInfo.getDbSID()).toString();
else
if(dbInfo.getDbType().equals("ORACLE"))
url = (newStringBuilder("jdbc:oracle:thin:@")).append(dbInfo.getDbHost()).append(":").append(dbInfo.getDbPort()).append(":").append(dbInfo.getDbSID()).toString();
else
if(dbInfo.getDbType().equals("ORACLE_DSP"))
url = (newStringBuilder("jdbc:dsp@")).append(dbInfo.getDbHost()).append(":").append(dbInfo.getDbPort()).append("/").append(dbInfo.getDbSID()).append("/").append(dbInfo.getNamespace()).toString();
else
if(dbInfo.getDbType().equals("SYBASE"))
url ="jdbc:sybase:Tds:...";
else
url ="sun.jdbc.odbc.JdbcOdbcDriver";
return url;
}
MySQL:
StringDriver="com.mysql.jdbc.Driver";//驱动程序
String URL="jdbc:mysql://localhost:3306/db_name"; //连接的URL,db_name为数据库名
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).new Instance();
Connectioncon=DriverManager.getConnection(URL,Username,Password);
Microsoft SQL Server 2.0驱动(3个jar的那个):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接SQL数据库的方法
StringURL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).new Instance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); //
Microsoft SQL Server 3.0驱动(1个jar的那个): // 老紫竹完善
StringDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //连接SQL数据库的方法
StringURL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).new Instance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); //
Sysbase:
StringDriver="com.sybase.jdbc.SybDriver";//驱动程序
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name为数据可名
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).newInstance();
Connectioncon=DriverManager.getConnection(URL,Username,Password);
Oracle(用thin模式):
StringDriver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl为数据库的SID
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).newInstance();//加载数据库驱动
Connection con=DriverManager.getConnection(URL,Username,Password);
PostgreSQL:
StringDriver="org.postgresql.Driver";//连接数据库的方法
String URL="jdbc:postgresql://localhost/db_name"; //db_name为数据可名
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).newInstance();
Connectioncon=DriverManager.getConnection(URL,Username,Password);
DB2:
StringDriver="com.ibm.db2.jdbc.app.DB2.Driver"; //连接具有DB2客户端的Provider实例
//StringDriver="com.ibm.db2.jdbc.net.DB2.Driver"; //连接不具有DB2客户端的Provider实例
String URL="jdbc:db2://localhost:5000/db_name"; //db_name为数据可名
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).newInstance();
Connectioncon=DriverManager.getConnection(URL,Username,Password);
Informix:
StringDriver="com.informix.jdbc.IfxDriver";
StringURL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name为数据可名
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).newInstance();
Connectioncon=DriverManager.getConnection(URL,Username,Password);
JDBC-ODBC:
StringDriver="sun.jdbc.odbc.JdbcOdbcDriver";
StringURL="jdbc:odbc:dbsource"; //dbsource为数据源名
String Username="username";//用户名
String Password="password";//密码
Class.forName(Driver).newInstance();
Connectioncon=DriverManager.getConnection(URL,Username,Password);