import java.util.*; import java.sql.*; public class MySQL { private Connection con=null; private String host="jdbc:mysql://127.0.0.1:3306/"; private String user; private String password; public MySQL(String db,String user,String password) { this.host=this.host+db; this.user=user; this.password=password; try { Class.forName("com.mysql.jdbc.Driver"); this.con=DriverManager.getConnection(host,user,password); } catch (Exception e) { System.err.println("数据库连接异常!");; } } /** * 数据库查询 */ public List selectAll(String table) { List list=new ArrayList<>(); try { String sql="select * from "+table; PreparedStatement pre=this.con.prepareStatement(sql); ResultSet res=pre.executeQuery(); ResultSetMetaData rsd = res.getMetaData(); int cols=rsd.getColumnCount(); while(res.next()) { HashMap map=new HashMap<>(); for (int i = 1; i <=cols; i++) { map.put(rsd.getColumnName(i),res.getString(i)); } list.add(map); } } catch (SQLException e) { System.err.println("selectAll方法异常!"); } return list; } /** * 添加数据 */ public boolean insert(String table,String paList1,String paList2) { boolean isSuc=false; try { String sql="insert into "+table+""+paList1+"values"+paList2; PreparedStatement pre=this.con.prepareStatement(sql); int resRow=pre.executeUpdate(); if(resRow>=1) { isSuc=true; } } catch (SQLException e) { System.err.println("insert方法异常!");; } return isSuc; } /** * 删除数据 */ public boolean delete(String table,String key,String value) { boolean isSuc=false; try { String sql="delete from "+table+" where "+key+"="+value; PreparedStatement pre=this.con.prepareStatement(sql); int resRow=pre.executeUpdate(); if(resRow>=1) { isSuc=true; } } catch (SQLException e) { System.err.println("delete方法异常!");; } return isSuc; } /** * 条件查询 */ public List selectIf(String table,String key,String value) { List list=new ArrayList<>(); try { String sql="select * from "+table+" where "+key+"="+value; PreparedStatement pre=this.con.prepareStatement(sql); ResultSet res=pre.executeQuery(); ResultSetMetaData rsd = res.getMetaData(); int cols=rsd.getColumnCount(); while(res.next()) { HashMap map=new HashMap<>(); for (int i = 1; i <=cols; i++) { map.put(rsd.getColumnName(i),res.getString(i)); } list.add(map); } } catch (SQLException e) { System.err.println("selectIf方法异常!"); } return list; } /** * 数据更新 */ public boolean updata(String table,String key,String Value,String newValue) { boolean isSuc=false; try { String sql="update "+table+" set "+key+"='"+newValue+"' where "+key+"='"+Value+"'"; PreparedStatement pre=this.con.prepareStatement(sql); int resRow=pre.executeUpdate(); if(resRow>=1) { isSuc=true; } } catch (SQLException e) { System.err.println("updata方法异常!");; } return isSuc; } /** * 数据库关闭操作 */ public void close() { if(con!=null) { try { con.close(); } catch (SQLException e) { System.err.println("数据库关闭异常!"); } } } /** * 查看整张表(调试) */ public void selectShow(String table) { List list=selectAll(table); for (int i = 0; i < list.size(); i++) { Map map=list.get(i); Set set = map.keySet(); Iterator iter = set.iterator(); while(iter.hasNext()) { String temp=iter.next(); System.out.print(map.get(temp)+"("+temp+")"+"\t"); } System.out.println(); } } }