package com.pkg1;
import java.sql.*;
public class JdbcDemo {
Connection conn = null;
//Statement st = null;
PreparedStatement st = null;
public JdbcDemo(){
this.init();
}
public boolean init(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //连接字符串
this.conn = DriverManager.getConnection("jdbc:odbc:test1", "sa", "123456"); //test1 odbc中的名称
//this.st = this.conn.createStatement();
}catch(Exception e){
e.printStackTrace();
this.close();
return false;
}
return true;
}
public int runCmd(String sql){
int ret = -1;
try {
this.st = this.conn.prepareStatement(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(st == null){
return ret;
}
try{
ret = st.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
return ret;
}
return ret;
}
//String sql = "select * from person where uid=?"
public ResultSet runQuery(String sql, String uid){
ResultSet rs = null;
try {
this.st = this.conn.prepareStatement(sql);
this.st.setString(1, uid); //设置参数
if(st == null){
return rs;
}
rs = st.executeQuery();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return rs;
}
return rs;
}
public void close(){
try{
if(st != null){
st.close();
}
if(conn != null){
conn.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}