這算是我第一個正式寫的java bean吧,歡迎取用修改,如果你覺得哪裡可以修改的更好,也歡迎留言給我,大恩大德感激不盡!
package com.dbcon.conn;
import java.sql.*;
/***********************************
使用需知: 傳入string database driver
beanid.setDriver(String dbdriver)
傳入string database connectionstring
beanid.setDBdata(String dbdata)
傳入sql執行命令
ResultSet rs = beanid.executeQuery(String sql)
//rs指標用完後要呼叫closeAll()全部關閉或closeRS()關閉指標 傳入sql更新命令 beanid.executeUpdate(String sql)
//更新命令
***********************************/
public class DBBean {
//宣告參數
Connection conn;
Statement stat;
ResultSet rs;
//建構子
public DBBean() { }
//宣告變數--驅動程式和資料庫連線字串,由網頁端傳送進來
private String myDriver = null;
private String myDBdata = null;
//取得驅動程式和資料庫連線字串
public void setDriver(String driver){
this.myDriver = driver;
}
public void setDBdata(String dbdata){
this.myDBdata = dbdata;
}
//執行查詢,回傳rs指標
public ResultSet executeQuery(String sql){
try{
Class.forName(myDriver);
conn = DriverManager.getConnection(myDBdata);
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stat.executeQuery(sql);
}catch(Exception ex1){
System.out.println(ex1.toString());
}
return rs;
}
//執行更新
public void executeUpdate(String sql){
try {
Class.forName(myDriver);
conn = DriverManager.getConnection(myDBdata);
stat=conn.createStatement();
stat.executeQuery(sql);
closeStat();
closeConn();
} catch (Exception ex2) {
System.out.println(ex2.toString());
}
}
public void closeAll(){
try{
rs.close();
closeStat();
closeConn();
}catch(Exception e1){
System.out.println(e1.toString());
}
}
public void closeRS(){
try{
rs.close();
}catch(Exception e2){
System.out.println(e2.toString());
}
}
public void closeStat(){
try{
stat.close();
}catch(Exception e3){
System.out.println(e3.toString());
}
}
public void closeConn(){
try{
conn.close();
}catch(Exception e4){
System.out.println(e3.toString());
}
}
}
import java.sql.*;
/***********************************
使用需知: 傳入string database driver
beanid.setDriver(String dbdriver)
傳入string database connectionstring
beanid.setDBdata(String dbdata)
傳入sql執行命令
ResultSet rs = beanid.executeQuery(String sql)
//rs指標用完後要呼叫closeAll()全部關閉或closeRS()關閉指標 傳入sql更新命令 beanid.executeUpdate(String sql)
//更新命令
***********************************/
public class DBBean {
//宣告參數
Connection conn;
Statement stat;
ResultSet rs;
//建構子
public DBBean() { }
//宣告變數--驅動程式和資料庫連線字串,由網頁端傳送進來
private String myDriver = null;
private String myDBdata = null;
//取得驅動程式和資料庫連線字串
public void setDriver(String driver){
this.myDriver = driver;
}
public void setDBdata(String dbdata){
this.myDBdata = dbdata;
}
//執行查詢,回傳rs指標
public ResultSet executeQuery(String sql){
try{
Class.forName(myDriver);
conn = DriverManager.getConnection(myDBdata);
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stat.executeQuery(sql);
}catch(Exception ex1){
System.out.println(ex1.toString());
}
return rs;
}
//執行更新
public void executeUpdate(String sql){
try {
Class.forName(myDriver);
conn = DriverManager.getConnection(myDBdata);
stat=conn.createStatement();
stat.executeQuery(sql);
closeStat();
closeConn();
} catch (Exception ex2) {
System.out.println(ex2.toString());
}
}
public void closeAll(){
try{
rs.close();
closeStat();
closeConn();
}catch(Exception e1){
System.out.println(e1.toString());
}
}
public void closeRS(){
try{
rs.close();
}catch(Exception e2){
System.out.println(e2.toString());
}
}
public void closeStat(){
try{
stat.close();
}catch(Exception e3){
System.out.println(e3.toString());
}
}
public void closeConn(){
try{
conn.close();
}catch(Exception e4){
System.out.println(e3.toString());
}
}
}
全站熱搜
留言列表