标签:jdbc
package com.yjm.ldapdao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.log4j.Logger; public class DAODB { Connection conn; PreparedStatement pst; ResultSet rs; public static Logger logger = Logger.getLogger(DAODB.class); public DAODB() { } public Connection conn() { try { if (conn == null) { if (LdapConf.log.equals("yes")) { logger.info("创建连接..."); } conn = DriverManager.getConnection(LdapConf.dburl, LdapConf.dbusername, LdapConf.dbpassword); } } catch (SQLException e) { logger.info("创建数据库连接失败..."); logger.info(e); e.printStackTrace(); } return conn; } public void closeConn() { try { if (rs != null) { rs.close(); if (LdapConf.log.equals("yes")) { logger.info("close rs"); } } if (pst != null) { pst.close(); if (LdapConf.log.equals("yes")) { logger.info("close pst"); } } if (conn != null) { conn.close(); logger.info("close conn"); } } catch (SQLException e) { logger.info(e); e.printStackTrace(); } } public PreparedStatement pre(String sql) { try { logger.info("创建 pst..."); pst = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); } catch (SQLException e) { e.printStackTrace(); } return pst; } public ResultSet rs() { try { logger.info("获得结果集..."); rs = pst.executeQuery(); } catch (SQLException e) { logger.info(e); e.printStackTrace(); } return rs; } public void closePreparedStatement() { try { if (pst != null) { pst.close(); logger.info("close pst"); } } catch (SQLException e) { e.printStackTrace(); } } public void closeResultSet() { try { if (rs != null) { rs.close(); logger.info("close rs"); } } catch (SQLException e) { logger.info(e); e.printStackTrace(); } } }
自己按要求写的jdbc 备份复用记载。
标签:jdbc
原文地址:http://yjm199.blog.51cto.com/4408395/1557375