• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

java实现的数据库管理类(mysql)_MySQL

mysql 搞代码 4年前 (2022-01-09) 74次浏览 已收录 0个评论

在我们使用数据库的时候,总会要写一个DBManager类来进行总体的数据库管理,在这里我们就要实现一个数据库管理类,这个是一个比较小型的数据库管理类,大体上实现了增删改查,在后面我们就会扩建这个数据库管理类,实现各种连接,来进行数据库的管理,好了,下面我们来看一下我们的代码:

<code class="hljs java">import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class DBManager {    /**     * 该方法用户连接数据库     *      * @return 返回Connection的一个实例     */    private Connection getConnection() {        Connection con = null;        try {            Class.forName(StaticVar.DRIVER_NAME).newInstance();            con = DriverManager.getConnection(StaticVar.DB_URL,                    StaticVar.USER_NAME, StaticVar.DB_PASSWD);        } catch (InstantiationException e) {            return null;        } catch (IllegalAccessException e) {            return null;        } catch (ClassNotFoundException e) {            return null;        } catch (SQLException e) {            return null;        }        return con;    }    /**     * 用于查询sql语句     *      * @param sql     *            sql语句     * @return 返回ResultSet集合     */    public ResultSet select(String sql) {        ResultSet res = null;        Connection con = getConnection();        Statement state = null;        if (!(con == null)) {            try {                state = con.createStatement();                res = state.executeQuery(sql);            } catch (SQLException e) {                return null;            }        }        if (state != null) {            try {                state.close();            } catch (SQLException e) {                return null;            }        }        if (con != null) {            try {                con.close();            } catch (SQLException e) {                return null;            }        }        return res;    }    /**     * 向表中插入一个元素,返回插入后的元素的id     *      * @param sql     * @return     */    public int insert(String sql) {        int iId = -1;        Connection con = getConnection();        Statement state = null;        if (con != null) {            try {                state = con.createStatement();                int res = state.executeUpdate(sql,                        Statement.RETURN_GENERATED_KEYS);                if (res != 0) {                    ResultSet rs = state.getGeneratedKeys();                    if (rs.next()) {                        iId = rs.getInt(1);                    }                }            } catch (SQLException e) {                iId = -1;            }        }        if (state != null) {            try {                state.close();            } catch (SQLException e) {            }        }        if (con != null) {            try {                con.close();            } catch (SQLException e) {            }        }        return iId;    }    /**     * 修改表中的某个元素的数值     *      * @param sql     *            sql语句     * @return 元素是否被成功修改     */    public boolean update(String sql) {        boolean updated = false;        Connection con = getConnection();        Statement state = null;        if (con != null) {            try {                state = con.createStatement();                int res = state.executeUpdate(sql);                if (res == 0) {                    updated = false;                } else {                    updated = true;                }            } catch (SQLException e) {                updated = false;            }        }        if (state != null) {            try {                state.close();            } catch (SQLException e) {            }        }        if (con != null) {            try {                con.close();            } catch (SQLException e) {            }        }        return updated;    }    /**     * 删除表中的某一个表项     *      * @param sql     *  <strong style="color:transparent">来源gaodai#ma#com搞@代~码$网</strong>          sql语句     * @return 返回是否删除成功     */    public boolean delete(String sql) {        boolean deleted = false;        Connection con = getConnection();        Statement state = null;        if (con != null) {            try {                state = con.createStatement();                int res = state.executeUpdate(sql);                if (res == 0) {                    deleted = false;                } else {                    deleted = true;                }            } catch (SQLException e) {                deleted = false;            }        }        if (state != null) {            try {                state.close();            } catch (SQLException e) {            }        }        if (con != null) {            try {                con.close();            } catch (SQLException e) {            }        }        return deleted;    }}</code>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java实现的数据库管理类(mysql)_MySQL
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址