首页>代码>Struts2实现的简单的用户增删改查实例>/userMg/src/cn/user/util/BaseDao.java
package cn.user.util;

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

public class BaseDao {

    private final String DRIVER="com.mysql.jdbc.Driver";
    private final String URL= "jdbc:mysql://localhost:3306/users";
    private final String USERNAME = "root";
    private final String PWD = "mysql";



    private Connection connection;
    private PreparedStatement ps;
    protected ResultSet rs;

    public Connection getConnection(){
        try {
            Class.forName(DRIVER);
            // 2在网络中查找数据库,创建连接对象
            connection = DriverManager.getConnection(
                    URL, USERNAME,PWD);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return connection;
    }

    public void closeAll(){
        try {
            // 释放资源
            if (rs != null)
                rs.close();
            if (ps != null)
                ps.close();
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 查询
     * @param sql
     * @param params
     */
    public void executeQuery(String sql , Object[] params){
        try {
            getConnection();
            ps = connection.prepareStatement(sql);
            if(params!= null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1, params[i]);
                }
            }

            rs = ps.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 查询
     * @param sql
     * @param params
     */
    public void executeQuery1(String sql , Object... params){
        try {
            getConnection();
            ps = connection.prepareStatement(sql);
            if(params!= null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1, params[i]);
                }
            }

            rs = ps.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 增、删、改
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String sql , Object[] params){
        int result = -1;
        try {
            getConnection();
            ps = connection.prepareStatement(sql);
            if(params!= null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1, params[i]);
                }
            }

            result= ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{

            this.closeAll();

        }
        return result;
    }


    /**
     * 增、删、改
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate1(String sql , Object... params){
        int result = -1;
        try {
            getConnection();
            ps = connection.prepareStatement(sql);
            if(params!= null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1, params[i]);
                }
            }

            result= ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{

            this.closeAll();

        }
        return result;
    }
    public ResultSet executeQueryRs(String sql , Object[] params){
        ResultSet rs = null;
        try {
            getConnection();
            ps = connection.prepareStatement(sql);
            if(params!= null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1, params[i]);
                }
            }

            rs = ps.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }
}
最近下载更多
1938953205  LV1 2022年5月18日
There,there  LV1 2021年12月14日
and123456  LV11 2021年10月22日
htdncccc  LV1 2021年5月31日
dangzhongan  LV3 2020年10月12日
yang2001  LV5 2020年9月16日
liuwenlong  LV20 2020年7月10日
haitao793  LV3 2020年7月9日
13035212816  LV1 2020年6月30日
qq2528732622  LV8 2020年6月15日
最近浏览更多
NHealers  LV5 2024年9月19日
Qolmen  LV12 2024年1月9日
uni-code_0123  LV1 2023年11月29日
wzh200808 2022年11月7日
暂无贡献等级
Yaolinchen 2022年9月26日
暂无贡献等级
谢谢谢谢谢谢你  LV6 2022年7月12日
lingtiejing  LV15 2022年6月10日
1938953205  LV1 2022年5月18日
1306878374  LV13 2022年4月11日
过过成功尺寸  LV7 2021年12月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友