jdbc 增删改查封装成方法 package com.zuipin.until;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaDat
package com.zuipin.until; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class DbUtil_IDUS { //分页属性 private int currpage = 1;// 当前页 private int pagesize = 5;// 每页条数 private int recordcount = 0;// 总条数 private int pagecount = 0;// 总页数 private Connection conn = null;//链接数据库对象 private String dbname = "users";//数据库名 private int port = 3306;//端口号 private String username = "root";//数据库名 private String pwd = "root";//数据库名 /** * 链接数据库 * @param database * @param port * @param name * @param pwd * @return */ public Connection connection(String database, int port, String name, String pwd) { Connection conn=null; try { //1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //建立连接 String url="jdbc:mysql://192.168.0.183:"+port+"/"+database+"?useUnicode=true&characterEncoding=utf8"; conn=DriverManager.getConnection(url, name, pwd); //4判断是否连接成功 if(!conn.isClosed()){ System.out.println("数据库连接成功"); }else { System.out.println("数据库连接失败"); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } /** * 获取表中主键 * @param tablename * @return */ public String getPk(String tablename) { String pk = null; DatabaseMetaData dbmd; try { dbmd = this.conn.getMetaData(); ResultSet rs = dbmd.getPrimaryKeys(this.dbname, null, tablename); if (rs.next()) { pk = rs.getString(4); } } catch (SQLException e) { e.printStackTrace(); } return pk; } //添加数据 /** * 以数组形式添加 1、sql语句 2.数组 * @param sql * @param values * @return */ public int add(String sql, Object[] values) { int num = 0; PreparedStatement pst; try { pst = this.conn.prepareStatement(sql); int i = 0; for (Object o : values) { pst.setObject(++i, o); } num = pst.executeUpdate(); pst.close(); } catch (SQLException e) { e.printStackTrace(); } return num; } /** * 以map添加数据 1.表名 2.map * @param tablename * @param m * @return */ public int insert(String tablename, Mapm) { int num = 0; StringBuilder n = new StringBuilder(); StringBuilder v = new StringBuilder(); for (String k : m.keySet()) { v.append("?,"); n.append(k + ","); } String sql = String.format("insert into %s(%s) values(%s)", tablename, n.toString().subSequence(0, n.length() - 1), v.toString().subSequence(0, v.length() - 1)); PreparedStatement pst; try { pst = this.conn.prepareStatement(sql); int i = 0; for (Object o : m.values()) { pst.setObject(++i, o); } num = pst.executeUpdate(); pst.close(); } catch (SQLException e) { e.printStackTrace(); } System.out.println(sql); return num; } //修改数据 /** * 以主键修改需要的字段 * @param tablename * @param m * @return */ public int update(String tablename, Map m) { int num = 0; String pk = this.getPk(tablename); if (m.containsKey(pk)) { num = update(tablename, m, pk + "=" + m.get(pk)); } else { num = update(tablename, m, "1=1"); } return num; } /** * 按条件修改需要的字段 * @param tablename * @param m * @param where * @return */ public int update(String tablename, Map m, String where) { int num=0; StringBuilder s = new StringBuilder(); for (String k : m.keySet()) { s.append(k + "=?,"); } String sql = String.format("update %s set %s where %s", tablename, s.toString().subSequence(0, s.length() - 1), where); PreparedStatement pst; try { pst = this.conn.prepareStatement(sql); int i = 0; for (Object o : m.values()) { pst.setObject(++i, o); } num = pst.executeUpdate(); pst.close(); } catch (SQLException e) { e.printStackTrace(); } return num; } public int zq(String name,String pwd){ int num=0; String sql = "UPDATE USER SET pwd='"+pwd+"' WHERE username='"+name+"'"; PreparedStatement pst; try { pst = this.conn.prepareStatement(sql); num = pst.executeUpdate(); pst.close(); } catch (SQLException e) { e.printStackTrace(); } return num; } //删除数据 /** * 删除表 * @param tablename * @return */ public int delete(String tablename) { int num = delete(tablename, "1=1"); return num; } /** * 以id删除数据 * @param tablename * @param id * @return */ public int deleteById(String tablename, Object id) { int num = delete(tablename, this.getPk(tablename) + "=" + id); return num; } /** * 以 where 条件删除数据 * @param tablename * @param where * @return */ public int delete(String tablename, String where) { int num = 0; String sql = String.format("delete from %s where %s", tablename, where); try { PreparedStatement pst = this.conn.prepareStatement(sql); num = pst.executeUpdate(); pst.close(); } catch (SQLException e) { e.printStackTrace(); } return num; } // 查询数据 /** * 以id查询某条数据 * @param tablename * @param id * @return */ public Map queryById(String tablename, Object id) { Map m = new HashMap (); String sql = String.format("select * from %s where %s", tablename, this.getPk(tablename) + "=" + id); try { PreparedStatement pst = this.conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); if (rs.next()) { ResultSetMetaData rsmd = rs.getMetaData(); int cc = rsmd.getColumnCount(); for (int i = 1; i <= cc; i++) { String name = rsmd.getColumnLabel(i); m.put(name, rs.getObject(name)); } } } catch (SQLException e) { e.printStackTrace(); } return m; } public List