连接数据库jdbc查询工具类

    xiaoxiao2023-10-13  174

    package com.aaa.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author lbw *2019年5月25日 * 类的注释 */ public class BaseDB { //建立连接 public Connection getConn(){ // Connection conn=null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.建立连接 conn=DriverManager.getConnection("jdbc:mysql:///lbw","root","root"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //建立增删改的通用的方法 @SuppressWarnings("all") public int addOrUpdate(String sql,Object... l){ // Connection c=getConn(); int j=0; PreparedStatement smt=null; //获取Statement接口 try { smt = c.prepareStatement(sql); //给占位符赋值 setPara(smt,l); //smt.setObject(, ); j=smt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭数据库的连接 //too many connections 连接数太多 CloseAll(null,smt,c); } return j; } /** * 查询的方法 * 方法的注释 * 2019年5月25日 */ public List<List<String>> query(String sql,Object... obj){ Connection conn=getConn(); PreparedStatement pst=null; //String sql=""; ResultSet rs =null; List<List<String>> l=new ArrayList<List<String>>(); try { pst=conn.prepareStatement(sql); //给参数赋值 setPara( pst,obj); rs = pst.executeQuery(); //获取列的总数的方法 //获取ResultSetMetaData接口 ResultSetMetaData rsm=rs.getMetaData(); //获取本次查询中 总的列数 int count=rsm.getColumnCount(); //System.out.println(i); while(rs.next()){ List<String> li=new ArrayList<String>(); for(int i=1;i<=count;i++){ //rs.getString(i); // System.out.print(rs.getString(i)+"--------"); li.add(rs.getString(i)); //{id="78",name="dd"} } l.add(li); } // System.out.println(l); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭 } return l; } /** * 返回一个List<Map> * 方法的注释 * 2019年5月25日 */ public List<Map<String,String>> queryMap(String sql,Object... obj){ Connection conn=getConn(); PreparedStatement pst=null; //String sql=""; ResultSet rs =null; List<Map<String,String>> l=new ArrayList<Map<String,String>>(); try { pst=conn.prepareStatement(sql); //给参数赋值 setPara(pst,obj); rs = pst.executeQuery(); //获取列的总数的方法 //获取ResultSetMetaData接口 jdbc的元数据 ResultSetMetaData rsm=rs.getMetaData(); //获取本次查询中 总的列数 int count=rsm.getColumnCount(); //System.out.println(i); while(rs.next()){ Map<String,String> m=new HashMap<String,String>(); for(int i=1;i<=count;i++){ //列名 值 //getColumnName 获取的是数据库的名字 //System.out.print(rsm.getColumnName(i)+"---"); //rs.getString(i) //rsm.getColumnLabel(i) 本次查询里面列的别名 m.put(rsm.getColumnLabel(i).toLowerCase(), rs.getString(i)); //System.out.print(rsm.getColumnLabel(i)+"----"); } //System.out.println(); l.add(m); } // System.out.println(l); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭 } return l; } /** * 给占位符赋值 * 方法的注释 * 2019年5月25日 */ public void setPara(PreparedStatement smt,Object...l){ if(l==null){ // }else{ //不为null开始赋值 for(int i=0;i<l.length;i++){ //占位符 的位置从1开始的 try { smt.setObject(i+1, l[i]); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //关闭的方法 public void CloseAll(ResultSet rs,Statement s,Connection c){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(s!=null){ try { s.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(c!=null){ try { c.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

     

    最新回复(0)