数据库封装类包含:连接数据库,更新,查询,是否有下一个,获取字段对应值,断开连接,get和set函数
可以说是非常完整的java操作数据库的一个封装类了,凡是涉及到数据库操作时,这个类用起来都非常方便。
DB.java
package javabean; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; public class DB { private String drv = "com.mysql.jdbc.Driver";//驱动 private String url = "jdbc:mysql://localhost:3306/Stu_cou";//根据自己的数据库设定 private String usr = "root";//用户名 private String pwd = "root";//密码 private Connection conn = null; private Statement stm = null; private ResultSet rs = null; //连接数据库 public boolean createConn() { boolean b = false; try { usr = "root"; pwd = "root"; Class.forName(drv).newInstance(); conn = (Connection)DriverManager.getConnection(url, usr, pwd); b = true; } catch (SQLException e) { } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } return b; } //数据库更新语句 public boolean update(String sql) { boolean b = false; try { stm = (Statement)conn.createStatement(); stm.execute(sql); b = true; } catch (Exception e) { System.out.println(e.toString()); } return b; } //查询语句 public void query(String sql) { try { stm = (Statement)conn.createStatement(); rs = stm.executeQuery(sql); } catch (Exception e) { } } //是否有下一个 public boolean next() { boolean b = false; try { if(rs.next())b = true; } catch (Exception e) { } return b; } //得到字段对应的值 public String getValue(String field) { String value = ""; try { if(rs!=null)value = rs.getString(field); } catch (Exception e) { e.printStackTrace(); } if (value == null) value = ""; return value; } //断开连接 public void close() { try { if (conn != null) conn.close(); } catch (SQLException e) { } try { if (stm != null) stm.close(); } catch (SQLException e) { } try { if (rs != null) rs.close(); } catch (SQLException e) { } } public void closeConn() { try { if (conn != null) conn.close(); } catch (SQLException e) { } } public void closeStm() { try { if (stm != null) stm.close(); } catch (SQLException e) { } } public void closeRs() { try { if (rs != null) rs.close(); } catch (SQLException e) { } } //get和set函数 public Connection getConn() { return conn; } public void setConn(Connection conn) { this.conn = conn; } public String getDrv() { return drv; } public void setDrv(String drv) { this.drv = drv; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public ResultSet getRs() { return rs; } public void setRs(ResultSet rs) { this.rs = rs; } public Statement getStm() { return stm; } public void setStm(Statement stm) { this.stm = stm; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsr() { return usr; } public void setUsr(String usr) { this.usr = usr; } }
注意:这个非本人原创!
DBC.java
这是自己写的一个,增加了PreparedStatement
package com.common.db; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Statement; public class DBC { private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/StaffRec"; private static String username = "root"; private static String password = "root"; static { try { /** * 加载驱动 */ Class.forName(driver); }catch(Exception ex) { ex.printStackTrace(); } } public static Connection getConnection() { /** * 创建连接对象 */ Connection conn=null; try { conn=(Connection) DriverManager.getConnection(url,username,password); }catch(Exception ex) { ex.printStackTrace(); } return conn; } /** * 关闭相关流 */ public static void close(ResultSet rs,Statement st,Connection conn) { try { if(rs!=null) { rs.close(); } if(st!=null) { st.close(); } if(conn!=null) { conn.close(); } }catch(SQLException e) { e.printStackTrace(); } } public static void closePst(ResultSet rs,PreparedStatement pst,Connection conn) { try { if(rs!=null) { rs.close(); } if(pst!=null) { pst.close(); } if(conn!=null) { conn.close(); } }catch(SQLException e) { e.printStackTrace(); } } }