import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class DBconnection {
// 連線資料庫
private Connection con = null;
private Statement stat = null;
protected ResultSet rs = null;
/*
*
* Oracle JAR包:Classes12.jar private final String DBDRIVER =
* "oracle.jdbc.driver.OracleDriver" ; private final String DBURL =
* "jdbc:oracle:thin:@IP:1521:資料名" ; private final String DBUSER = "使用者名稱" ;
* private final String DBPASSWORD = "密碼" ; private Connection conn=null ;
*
* //SQL Server三個JAR包 mssqlserver.jar,msbase.jar,msutil.jar private final
* String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
* private final String DBURL =
* "jdbc:microsoft:sqlserver://IP:1433;DatabaseName=資料庫名;" ; private final
* String DBUSER = "使用者名稱" ; private final String DBPASSWORD = "密碼" ; private
* Connection conn=null ;
*
* //SQL ServerJDBC-ODBC橋 private final String DBDRIVER =
* "sun.jdbc.odbc.JdbcOdbcDriver" ; private final String DBURL =
* "jdbc:odbc:資料來源名" ; private final String DBUSER = "使用者名稱" ; private final
* String DBPASSWORD = "密碼" ; private Connection conn=null ;
*
* //MySQL JAR包 mysql-connector-java-3.1.12-bin.jar private final String
* DBDRIVER = "org.gjt.mm.mysql.Driver" ; private final String DBURL =
* "jdbc:mysql://IP /資料庫名" ; private final String DBUSER = "使用者名稱" ; private
* final String DBPASSWORD = "密碼" ; private Connection conn=null ;
*
* //DB2 private final String DBDRIVER = "com.ibm.db2.jdbc.app.DB2Driver" ;
* private final String DBURL = "jdbc:db2://IP:5000/資料庫名" ; private final
* String DBUSER = "使用者名稱" ; private final String DBPASSWORD = "密碼" ; private
* Connection conn=null ;
*/
public Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://192.168.0.1:1433;"
+ "databaseName=hos;user=username;password=password";
con = DriverManager.getConnection(url);
con.setAutoCommit(false);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// 得到結果集
public ResultSet getResultSet(String sql) {
if (con == null) {
con = this.getConnection();
}
try {
stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stat.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
// 插入和修改資料庫
public int updateDataBase(String sql, Connection con) {
int i = -1;
try {
stat = con.createStatement();
i = stat.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
// 關閉連線
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stat != null) {
stat.close();
stat = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}