📄 dbconnect.jsp
字号:
<%@ page import="java.util.*,java.sql.*, java.io.*" %><%! //com.isavvix.ijc.util.* class DBConnect { private ResultSet rs = null ; private Statement stmt = null; private Connection conn = null ; private String dbUser=null; private String dbPswd=null; private String dbClass=null; private String dbString=null; //dbClass="org.gjt.mm.mysql.Driver"; //dbString="jdbc:mysql://localhost:3306/test"; //建立默认的Connetion对象 public DBConnect(String dbString,String dbClass,String dbUser,String dbPswd) throws SQLException{ this.dbString=dbString; this.dbClass=dbClass; this.dbUser=dbUser; this.dbPswd=dbPswd; this.getConnection(); } //获得其他连接对象 public Connection getConnection() throws SQLException { try{ if(conn==null){ //if(member==null)member = new Member(dbUser,dbPswd); //conn = member.getConnection(); Class.forName(dbClass); conn = DriverManager.getConnection(dbString,dbUser,dbPswd); } } catch (Exception e){ ///////////////////////////////////////// //System.out.println("Exception"); } finally{ //try { conn.close(); } catch (Exception e) {} } return conn; } //获得statement对象 public Statement getStatment() throws SQLException { if(conn == null) conn = getConnection(); return conn.createStatement(); } //获得带参数的statement对象 public void setStatment() throws SQLException { this.stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); } //获得PreparedStatement对象 public PreparedStatement getPrepareStatment(String SqlStr) throws SQLException { return conn.prepareStatement(SqlStr); } //获得CallableStatement对象;执行存储过程用 public CallableStatement getCallableStatement(String SqlStr) throws SQLException { return conn.prepareCall(SqlStr); } //执行检索式SQL语句 public synchronized ResultSet doSearch(String sql_in) { if (sql_in!=null){ try{ //检查数据库连接是否还存在 if(conn == null) conn = getConnection(); if(stmt == null) stmt =getStatment(); rs = stmt.executeQuery(sql_in); } catch(Exception e){ e.printStackTrace(); } return rs; } else{ return null; } } //执行更新式SQL语句 public synchronized int doUpdate(String sql_in) { int updateResult=-1; if (sql_in != null) { sql_in = UnicodeToGBK(sql_in); try { //检查数据库连接是否还存在 if(conn == null) conn = getConnection(); stmt = this.getStatment(); updateResult = stmt.executeUpdate(sql_in); } catch(Exception e) { e.printStackTrace(); } } return updateResult; } //批处理批行 sql语句 by chen xiao dong public synchronized int doUpdates(String[] sql_in) { int updateResult=0; if (sql_in!=null) { try { boolean oldCommit = conn.getAutoCommit(); if ( oldCommit ) conn.setAutoCommit(false); stmt =this.getStatment(); for (int i = 0; i < sql_in.length; i++) { if ( stmt.executeUpdate(sql_in[i]) > 0 ) updateResult = updateResult + 1; } conn.commit(); if ( oldCommit ) conn.setAutoCommit(true); } catch(Exception e) { e.printStackTrace(); } } return updateResult; } //获取纪录总数 public int getRowCount(String Sql) { int result_code = -1; String stmSql = null; try { if(conn == null) conn = getConnection(); if(Sql != null) { int pos = Sql.indexOf(" From "); if(pos < 0) pos = Sql.indexOf(" from "); if(pos < 0) pos = Sql.indexOf(" FROM "); if(pos > 0) { String buf = Sql.substring(pos); stmSql = "Select Count(*) " + buf; } if(stmSql != null) { Statement stm2 = getStatment(); ResultSet rs2 = stm2.executeQuery(stmSql); rs2.next(); result_code = rs2.getInt(1); rs2.close(); stm2.close(); rs2 = null; stm2 = null; } } } catch(Exception e) { e.printStackTrace(); } return result_code; } //关闭数据库连接 public void close() { try { if(rs!=null) rs = null; if(stmt!=null){ stmt.close(); stmt = null; } if(conn!=null){ if (!conn.isClosed()) conn.close(); conn = null; } }catch(SQLException e){e.printStackTrace();} } //释放对象 public void destroy(){ this.close(); } //检查连接是否还存在,成功返回 True public boolean isALive() { if(conn != null) return true; else return false; } };%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -