[java]代码库
import java.sql.*;
public class JDBConnection {
private String dbDriver="sun.jdbc.odbc.JdbcOdbcDriver";//驱动名
private String url = "jdbc:odbc:dbname";//连接数据库
private Connection con;
public JDBConnection(){//加载数据库的驱动
try{
Class.forName(dbDriver).newInstance();
}catch(Exception e){
e.printStackTrace();
}
}
/*
* 连接数据库
* */
public boolean creatConnection(){
try{
con = DriverManager.getConnection(url);
con.setAutoCommit(true);
}catch(Exception e1){
e1.printStackTrace();
}
return true;
}
/*
* 数据库数据的查找
* */
public ResultSet executeQuery(String sql){
ResultSet rs = null;
try{
if(con==null){
creatConnection();
}
Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql);
}catch(Exception e2){
e2.printStackTrace();
}
return rs;
}
/*
* 数据库的添加 修改 删除
* */
public boolean executeUpdate(String sql){
try{
if(con==null){
creatConnection();
}
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
}catch(Exception e3){
e3.printStackTrace();
}
return true;
}
/*
* 关闭连接
* */
public void closeConnection(){
if(con==null){
try{
con.close();
}catch(Exception e4){
e4.printStackTrace();
}
}
}
}