JAVA数据库操作类
作者:admin 日期:2008-11-02
import java.sql.*;
public class conn{
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:student";
Connection connect=null;
ResultSet rs=null;
//构造方法加载数据库驱动
public conn(){
try{
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
//执行查询语句
public ResultSet executeQuery(String sql){
try{
connect=DriverManager.getConnection(sConnStr);
Statement stmt=connect.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
//执行插入,更新.修改语句
public int executeUpdate(String sql){
int rs=0;
try{
connect=DriverManager.getConnection(sConnStr);
Statement stmt=connect.createStatement();
rs=stmt.executeUpdate(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
//执行汉化语句
public String tostr(String str){
if(str==null){
str="";
}else{
try{
str=(new String(str.getBytes("iso-8859-1"),"gb2312"));
}catch(Exception e){
System.out.println(e.getMessage());
}
}
return str;
}
}
public class conn{
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:student";
Connection connect=null;
ResultSet rs=null;
//构造方法加载数据库驱动
public conn(){
try{
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
//执行查询语句
public ResultSet executeQuery(String sql){
try{
connect=DriverManager.getConnection(sConnStr);
Statement stmt=connect.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
//执行插入,更新.修改语句
public int executeUpdate(String sql){
int rs=0;
try{
connect=DriverManager.getConnection(sConnStr);
Statement stmt=connect.createStatement();
rs=stmt.executeUpdate(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
//执行汉化语句
public String tostr(String str){
if(str==null){
str="";
}else{
try{
str=(new String(str.getBytes("iso-8859-1"),"gb2312"));
}catch(Exception e){
System.out.println(e.getMessage());
}
}
return str;
}
}
评论: 0 | 引用: 0 | 查看次数: 2043
发表评论
你没有权限发表留言!