JDBC 链接案例—— 纯代码,无连接池
2015-03-08 21:09
253 查看
public class BaseDao { private String driver = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/mydb"; private String user = ""; private String password = ""; Connection conn = null; /** * 获取数据库链接对象 */ public Connection getConnection(){ if(conn == null){ try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); } catch (Exception e) { e.printStackTrace(); } } return conn; } /** * 增、删、改操作 */ public int executeUpdate(String sql, Object[] params){ PreparedStatement pstmt = null; int num = 0; conn = getConnection(); try { pstmt = conn.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { pstmt.setObject(i+1, params[i]); } } num = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally{ closeAll(conn,pstmt, null); } } /** * 关闭数据库链接 */ public void closeAll(Connection conn, Statement stmt, ResultSet rs){ if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
相关文章推荐
- JDBC 连接池代码
- JDBC实现简单的连接池代码
- 微信扫描打开APP下载链接提示代码案例源码
- JDBC连接池:自定义连接池代码实现
- servler+jsp+jdbc的分页简单案例(链接oracle)
- JDBC通过连接池链接数据库总是失败的解决办法
- java JDBC链接数据库代码以及步骤
- 创建JDBC模板简化代码、JDBC应用的事务管理以及连接池的作用
- J2SE基础夯实系列之JDBC,链接数据库实例代码
- JDBC链接Oracle参考代码
- 关于JDBC链接数据库的代码实现
- jdbc操作数据库的代码案例
- 完整java开发中JDBC链接数据库代码和步骤
- JSP案例:之与各种数据库链接的案例代码
- JDBC链接数据库及使用代码
- JDBC链接数据库的过程及代码
- jdbc链接到数据库的示例代码
- JDBC第一天连接池案例
- JDBC、DBCP、C3P0、jdbc-pool--链接方式?连接池?
- 数据库_jdbc_连接池+框架_改造分页案例