logo资料库

设备管理系统源代码.doc

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
设备管理系统源代码 package com.DB; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBManager { private final static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; private final static String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DataBaseName=bbsDB"; private Connection conn; private Statement st; private ResultSet rs; /** * 创建连接 * @return */ public Connection getConnection(){ if(conn==null){ try { Class.forName(driver);
conn = DriverManager.getConnection(url,"sa","root"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return conn; } /** * 创建 statement */ public Statement getStatement(){ if(st==null){ try { st = conn.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st;
} /** * 执行更新操作 */ public int update(String sql){ System.out.println(">>>>>>>>===="+sql); getConnection(); int rows=0; st=getStatement(); try { rows = st.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(); } return rows; } /** * 使用预处理 */ public int update(String sql,String data[]){ System.out.println(">>>>==="+sql); getConnection();
try { int index = 0; PreparedStatement pst = conn.prepareStatement(sql); for (int i = 0; i < data.length; i++) { pst.setString(++index, data[i]); } return pst.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(); } return 0; } /** * 执行查询操作 */ public ResultSet getQuery(String sql){ System.out.println(">>>>>>>>===="+sql); getConnection(); st = getStatement(); try { rs = st.executeQuery(sql); } catch (SQLException e) {
e.printStackTrace(); } return rs; } /** * 执行关闭 */ public void closeAll(){ if(rs!=null){ try { rs.close(); rs = null; } catch (SQLException e) { e.printStackTrace(); } } if(st!=null){ try { st.close(); st = null; } catch (SQLException e) { e.printStackTrace(); } }
if(conn!=null){ try { conn.close(); conn = null; } catch (SQLException e) { e.printStackTrace(); } } } }
分享到:
收藏