Posted on 2009-06-15 22:40
London2012 阅读(135)
评论(0) 编辑 收藏 所属分类:
CODE
package util;
import java.sql.*;
public class DBHelper {
private static String dbdriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static String dbname="jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=db_library";
private static String dbuser="sa";
private static String dbpwd="admin";
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
public DBHelper(){
try {
Class.forName(dbdriver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConn() throws SQLException{
if(conn==null){
return conn=DriverManager.getConnection(dbname,dbuser,dbpwd);
}
else
return conn;
}
public int getretuenInt(String sql){
int i=0;
try {
rs=getRs(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
while(rs.next()){
i++;
}
System.out.println("i="+i);
return i;
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
public Statement getStmt() throws SQLException{
stmt=conn.createStatement();
return stmt;
}
public ResultSet getRs(String sql) throws SQLException{
rs=getStmt().executeQuery(sql);
return rs;
}
public int executeUpdate(String sql){
try {
return getStmt().executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
public void CloseAll(){
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DBHelper fb=new DBHelper();
try {
System.out.println(fb.getConn());
} catch (SQLException e) {
e.printStackTrace();
}
}
}