在我们使用数据库的时候,总会要写一个DBManager类来进行总体的数据库管理,在这里我们就要实现一个数据库管理类,这个是一个比较小型的数据库管理类,大体上实现了增删改查,在后面我们就会扩建这个数据库管理类,实现各种连接,来进行数据库的管理,好了,下面我们来看一下我们的代码:
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class DBManager { 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; } 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; } 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; } 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; } 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";}
这样我们的一个小型的数据库管理类就实现了,至于后面的扩展,我们会在后面的博客中讲到。