package beans;
import java.sql.*;
public class connDB{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
public connDB(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
/***************************************************
*method name: executeQuery()
*功能:执行查询操作
*return value: ResultSet
*2007-03-05
****************************************************/
public ResultSet executeQuery(String sql){
try{
conn=DriverManager.getConnection("jdbc:odbc:db_bookmanage");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
/***************************************************
*method name: executeUpdate()
*功能:执行更新操作
*return value: int
*2007-03-05
****************************************************/
public int executeUpdate(String sql){
int result=0;
try{
conn=DriverManager.getConnection("jdbc:odbc:db_bookmanage");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch(SQLException ex){
result=0;
}
return result;
}
/***************************************************
*method name:executeUpdate_id()
*功能:执行更新操作并返回自动编号的值
*return value: int
*2007-03-05
****************************************************/
public int executeUpdate_id(String sql) {
int result = 0;
try{
conn=DriverManager.getConnection("jdbc:odbc:db_bookmanage");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql);
String ID = "select @@IDENTITY as id";
rs = stmt.executeQuery(ID);
if (rs.next()) {
int autoID = rs.getInt("id");
result = autoID;
}
}
catch (SQLException ex) {
result = 0;
}
return result;
}
/***************************************************
*method name: close()
*功能:关闭数据库链接
*return value: void
*2007-03-05
****************************************************/
public void close() {
try {
if (rs != null) {
rs.close(); //关闭ResultSet结果集
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (stmt != null) {
stmt.close(); //关闭Statement
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (conn != null) {
conn.close(); //关闭Connection
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
}