public static Connection getConnection(){
Connection conn=null;
private static String dbUrl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_shop”;
private static String dbUser="sa";
private static String dbPwd="";
private static String dbClassName=“com.microsoft.jdbc.sqlserver.SQLServerDriver";
// loadProperty();//通过loadProperty方法读取properties文件中的数据库连接所需信息
try{
Class.forName(dbClassName).newInstance();
conn=DriverManager.getConnection(dbUrl,dbUser,dbPwd);
}catch(Exception ee){
ee.printStackTrace();
}
if(conn=null){
System.err.println("警告:DbConnectionManager.getConnection() 获得数据库连接失败。\r\n\r\n连接;类型:"+dbClassName+"\r\n连接位置:"+dbUrl+"\r\n用户/密码"+dbUser+"/"+dbPwd);
}
return conn;
}
public ResultSet executeQuery(String sql){
try{
conn=getConnection();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.out.println(ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql){
int result=0;
try{
conn=getConnection();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch(SQLException ex){
result =0;
}try{
stmt.close();
}catch(SQLException ex1){
return result;
}
public void close(){
try{
if(rs!=null){
rs.close();
}
}catch(Exception e){
e.printStackTrace(System.err);}
try{
if(stmt!=null){
stmt.close();
}
}catch(Exception e){
e.printStackTrace(System.err);
}
try{
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace(System.err);
}
}
}