设备管理系统源代码.doc
《设备管理系统源代码.doc》由会员分享,可在线阅读,更多相关《设备管理系统源代码.doc(6页珍藏版)》请在冰点文库上搜索。
packagecom.DB;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
publicclassDBManager{
privatefinalstaticStringdriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
privatefinalstaticStringurl="jdbc:
microsoft:
sqlserver:
//127.0.0.1:
1433;DataBaseName=bbsDB";
privateConnectionconn;
privateStatementst;
privateResultSetrs;
/**
*创建连接
*@return
*/
publicConnectiongetConnection(){
if(conn==null){
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,"sa","root");
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
returnconn;
}
/**
*创建statement
*/
publicStatementgetStatement(){
if(st==null){
try{
st=conn.createStatement();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
returnst;
}
/**
*执行更新操作
*/
publicintupdate(Stringsql){
System.out.println(">>>>>>>>===="+sql);
getConnection();
introws=0;
st=getStatement();
try{
rows=st.executeUpdate(sql);
}catch(SQLExceptione){
e.printStackTrace();
}finally{
closeAll();
}
returnrows;
}
/**
*使用预处理
*/
publicintupdate(Stringsql,Stringdata[]){
System.out.println(">>>>==="+sql);
getConnection();
try{
intindex=0;
PreparedStatementpst=conn.prepareStatement(sql);
for(inti=0;i pst.setString(++index,data[i]);
}
returnpst.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
closeAll();
}
return0;
}
/**
*执行查询操作
*/
publicResultSetgetQuery(Stringsql){
System.out.println(">>>>>>>>===="+sql);
getConnection();
st=getStatement();
try{
rs=st.executeQuery(sql);
}catch(SQLExceptione){
e.printStackTrace();
}
returnrs;
}
/**
*执行关闭
*/
publicvoidcloseAll(){
if(rs!
=null){
try{
rs.close();
rs=null;
}catch(SQLExceptione){
e.printStackTrace();
}
}
if(st!
=null){
try{
st.close();
st=null;
}catch(SQLExceptione){
e.printStackTrace();
}
}
if(conn!
=null){
try{
conn.close();
conn=null;
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
}