思路:先建立一个DBUtil类,管理所有的创建连接、关闭连接、提交事务、回滚事务、关闭Statement、关闭Resultset等操作。
然后编写一个Client类结合DBUtil类即可,思路非常简洁。
DBUtil类代码:
package custom; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库工具类 * @author Administrator * */ public class DBUtil { /** * 取得数据库连接 * @return */ public static Connection getConnection() { Connection conn = null; try { //取得jdbc配置信息 String filePath=DBUtil.class.getClassLoader().getResource("").getPath()+"conf.properties"; String url=PropertiesDAO.getProperties(filePath, "url"); String userName=PropertiesDAO.getProperties(filePath, "userName"); String password=PropertiesDAO.getProperties(filePath, "password"); String driverName=PropertiesDAO.getProperties(filePath, "driverName"); Class.forName(driverName); conn = DriverManager.getConnection(url, userName, password); } catch (ClassNotFoundException e) { //记录日志可以将类不能找记录进去,这样可以更准确的定位问题 //但是给用户不应该抛出类不能找到,应该抛出用户能够理解的错误 e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static void close(PreparedStatement pstmt) { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void commit(Connection conn) { if (conn != null) { try { conn.commit(); } catch (SQLException e) { e.printStackTrace(); } } } public static void rollback(Connection conn) { if (conn != null) { try { conn.rollback(); } catch (SQLException e) { e.printStackTrace(); } } } public static void setAutoCommit(Connection conn, boolean autoCommit) { if (conn != null) { try { conn.setAutoCommit(autoCommit); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Statement stmt) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { DBUtil.getConnection(); System.out.println("---------ok---------"); } }
Client类的requestBegin方法:
public void requestBegin(SCESession mySession) { // TODO Auto-generated method stub super.requestBegin(mySession); Connection conn=null; PreparedStatement pre=null; try { conn=DBUtil.getConnection(); //begin transaction DBUtil.setAutoCommit(conn, false); //get mobile IVariable mobileVar = mySession.getVariable(IProjectVariables.PROMPT_COLLECT_MOBILE); String mobile=mobileVar.getComplexVariable().getField(IProjectVariables.PROMPT_COLLECT_MOBILE_FIELD_VALUE).getStringValue(); //get ready for the sql String sql="insert into satisfaction(mobile) values(?)"; pre = conn.prepareStatement(sql);// 实例化预编译语句 pre.setString(1, mobile); pre.executeUpdate(); DBUtil.commit(conn); } catch (Exception e) { e.printStackTrace(); //rollback DBUtil.rollback(conn); }finally{ DBUtil.close(pre); DBUtil.close(conn); } }
以上,大功告成!
扫描二维码关注公众号,回复:
1272100 查看本文章
refurl:
http://www.cnblogs.com/liuxianan/archive/2012/08/05/2624300.html