package
dbconn;
import java.sql. * ;
public class DBResult
... {
private Connection con;
Statement stmt;
ResultSet rs = null;
public DBResult()...{
try ...{
this.con=getConnection();
} catch (Exception e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception...{
Connection con=null;
try...{
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con=DriverManager.getConnection("jdbc:odbc:mycms","sa","");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_shopping;User=sa;Password=");
}catch(ClassNotFoundException e)...{
System.out.println(e.getMessage());
}catch(SQLException e)...{
System.out.println(e.getMessage());
}
if(con==null)
System.out.println("error");
return con;
}
/** *//**
* 用于获得执行SQL语句的ResultSet对象
*/
public ResultSet getResult(String sql)...{
try...{
con=getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}
catch(Exception e)...{}
return rs;
}
/** *//**
* 用于执行SQL语句没有返回值
*/
public int executeUpdate(String sql) throws SQLException...{
int result=0;
try ...{
con=getConnection();
stmt=con.createStatement();
stmt.executeUpdate(sql);
} catch (Exception e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result;
}
/** *//**
* 用于获得执行SQL语句的PreparedStatement(预处理)对象
*/
public PreparedStatement getPreparedStatement(String sql)...{
try...{
con=getConnection();
PreparedStatement pstmt=con.prepareStatement(sql);
return pstmt;
}
catch(Exception e)...{}
return null;
}
/** *//**
* 关闭连接
*/
public void closeCon()...{
try...{
this.con.close();
}catch(Exception e)...{
e.printStackTrace();
}
}
}
import java.sql. * ;
public class DBResult
... {
private Connection con;
Statement stmt;
ResultSet rs = null;
public DBResult()...{
try ...{
this.con=getConnection();
} catch (Exception e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception...{
Connection con=null;
try...{
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con=DriverManager.getConnection("jdbc:odbc:mycms","sa","");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_shopping;User=sa;Password=");
}catch(ClassNotFoundException e)...{
System.out.println(e.getMessage());
}catch(SQLException e)...{
System.out.println(e.getMessage());
}
if(con==null)
System.out.println("error");
return con;
}
/** *//**
* 用于获得执行SQL语句的ResultSet对象
*/
public ResultSet getResult(String sql)...{
try...{
con=getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}
catch(Exception e)...{}
return rs;
}
/** *//**
* 用于执行SQL语句没有返回值
*/
public int executeUpdate(String sql) throws SQLException...{
int result=0;
try ...{
con=getConnection();
stmt=con.createStatement();
stmt.executeUpdate(sql);
} catch (Exception e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result;
}
/** *//**
* 用于获得执行SQL语句的PreparedStatement(预处理)对象
*/
public PreparedStatement getPreparedStatement(String sql)...{
try...{
con=getConnection();
PreparedStatement pstmt=con.prepareStatement(sql);
return pstmt;
}
catch(Exception e)...{}
return null;
}
/** *//**
* 关闭连接
*/
public void closeCon()...{
try...{
this.con.close();
}catch(Exception e)...{
e.printStackTrace();
}
}
}