程序源码:import java.sql.*;import java.io.*;public class DBBean implements Serializable{
private String DBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String DBLocation = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sms_database";
private String LogName = "sa";
private String Password= "";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
String strHint = ""; public void DBBean() { } public void setDBDriver(String driver){ DBDriver = driver;}
public String getDBDriver(){ return DBDriver; } public void setDBLocation(String location){ DBLocation = location;}
public String getDBLocation(){ return DBLocation; } public void setLogName(String logname){ LogName = logname;} public void setPassword(String password){ Password = password;} public String getHint(){return strHint;} public void initBean(){
try { Class.forName(DBDriver); }
catch(java.lang.ClassNotFoundException e) {
System.err.println("initBean(): " + e.getMessage());
}
} public Connection getConn(){
try { conn = DriverManager.getConnection(DBLocation,LogName,Password); }
catch(SQLException ex) {
System.err.println("getConn(): " + ex.getMessage());
} return conn;
}//~getConn public ResultSet executeQuery(String sql){
rs = null;
try{
conn = getConn();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
strHint = "executeQuery: " + ex.getMessage();
System.err.println(strHint);
}
return rs;
} //execute a SQL statement without returning recordset
public boolean executeUpdate(String sql)
{
try
{
conn = getConn();
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
}
catch(SQLException ex)
{
strHint = "executeUpdate: " + ex.getMessage();
System.err.println(strHint);
return false;
}
} //Close connections
public boolean closeConn()
{ try
{
if (rs!=null)
rs.close();
if (stmt!=null)
stmt.close();
if (conn!=null)
conn.close();
return true;
}
catch(SQLException ex)
{
strHint = "closeConn: " + ex.getMessage();
System.err.println(strHint);
return false;
}
}}//~class
private String DBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String DBLocation = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sms_database";
private String LogName = "sa";
private String Password= "";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
String strHint = ""; public void DBBean() { } public void setDBDriver(String driver){ DBDriver = driver;}
public String getDBDriver(){ return DBDriver; } public void setDBLocation(String location){ DBLocation = location;}
public String getDBLocation(){ return DBLocation; } public void setLogName(String logname){ LogName = logname;} public void setPassword(String password){ Password = password;} public String getHint(){return strHint;} public void initBean(){
try { Class.forName(DBDriver); }
catch(java.lang.ClassNotFoundException e) {
System.err.println("initBean(): " + e.getMessage());
}
} public Connection getConn(){
try { conn = DriverManager.getConnection(DBLocation,LogName,Password); }
catch(SQLException ex) {
System.err.println("getConn(): " + ex.getMessage());
} return conn;
}//~getConn public ResultSet executeQuery(String sql){
rs = null;
try{
conn = getConn();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
strHint = "executeQuery: " + ex.getMessage();
System.err.println(strHint);
}
return rs;
} //execute a SQL statement without returning recordset
public boolean executeUpdate(String sql)
{
try
{
conn = getConn();
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
}
catch(SQLException ex)
{
strHint = "executeUpdate: " + ex.getMessage();
System.err.println(strHint);
return false;
}
} //Close connections
public boolean closeConn()
{ try
{
if (rs!=null)
rs.close();
if (stmt!=null)
stmt.close();
if (conn!=null)
conn.close();
return true;
}
catch(SQLException ex)
{
strHint = "closeConn: " + ex.getMessage();
System.err.println(strHint);
return false;
}
}}//~class
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货