zoukankan      html  css  js  c++  java
  • 基础BaseDao

    package cn.easybuy.util;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import cn.easybuy.tool.ConfigManager;

    /**
    * Dao层的公共属性和方法
    *
    */
    public class BaseDao {

    /* 连接数据库 */
    public static Connection getConnection() {

    // 连接数据库的四要素
    String url = ConfigManager.getInstance().getValue("url");
    String user = ConfigManager.getInstance().getValue("user");
    String password = ConfigManager.getInstance().getValue("password");
    String driver = ConfigManager.getInstance().getValue("driverClass");
    Connection connection = null;

    try {
    Class.forName(driver);
    connection = DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return connection;

    }

    public static boolean closeResource(Connection connection,
    PreparedStatement ps, ResultSet rs) {
    boolean flag = true;
    if (rs != null) {
    try {
    rs.close();
    rs = null;// GC回收
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    flag = false;
    }

    }

    if (ps != null) {
    try {
    ps.close();
    ps = null;
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    flag = false;
    }

    }
    if (connection != null) {
    try {
    connection.close();
    connection = null;
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    flag = false;
    }

    }
    return flag;

    }

    /**
    * 查询操作
    *
    * @throws Exception
    */
    public static ResultSet execute(Connection connection,
    PreparedStatement ps, ResultSet rs, String sql, Object[] params)
    throws Exception {
    ps = connection.prepareStatement(sql);
    for (int i = 0; i < params.length; i++) {
    ps.setObject(i + 1, params[i]);
    }
    rs = ps.executeQuery();
    return rs;
    }

    /**
    * 更新操作
    *
    * @throws Exception
    */
    public static int execute(Connection connection, PreparedStatement ps,
    String sql, Object[] params) throws Exception {
    int updateRows = 0;
    ps = connection.prepareStatement(sql);
    for (int i = 0; i < params.length; i++) {
    ps.setObject(i + 1, params[i]);
    }
    updateRows = ps.executeUpdate();
    return updateRows;

    }
    }

  • 相关阅读:
    Sql Server 存储过程删除一个表里(除ID外)完全重复的数据记录
    把一个库中的表复制到另外一个库的表中(Sql server 2005)
    ajax执行后台返回的提交表单及JS
    WinCE中使用本地数据库SQLite以及得到当前应用程序所在路径
    如何评测一个软件工程师的计算机网络知识水平与网络编程技能水平
    如何评测软件工程知识技能水平?
    深入理解TCP协议及其源代码
    Socket与系统调用深度分析
    创新产品的需求分析:未来的图书会是什么样子?
    ubuntu小问题集合
  • 原文地址:https://www.cnblogs.com/javaxiaodoufu/p/7464601.html
Copyright © 2011-2022 走看看