您好,欢迎访问一九零五行业门户网

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

在我们使用数据库的时候,总会要写一个dbmanager类来进行总体的数据库管理,在这里我们就要实现一个数据库管理类,这个是一个比较小型的数据库管理类,大体上实现了增删改查,在后面我们就会扩建这个数据库管理类,实现各种连接,来进行数据库的管理,好了,下面我们来看一下我们的代码:
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 * 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; }}
下面看一下其中用到的一个类staticvar。
staticvar.java
public class staticvar { public static final string db_url = jdbc:mysql://localhost/db_test?useunicode=true&characterencoding=utf-8; public static final string user_name = chen; public static final string db_passwd = ******; public static final string driver_name = com.mysql.jdbc.driver;}
这样我们的一个小型的数据库管理类就实现了,至于后面的扩展,我们会在后面的博客中讲到。
其它类似信息

推荐信息