package Dao; import Bean.Menu; import Bean.bean; import Bean.policy; import DBUtil.dbutil; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.Reader; import java.sql.*; import java.util.ArrayList; import java.util.List; public class dao { public List<bean> searchAll( ){ List<bean> list = new ArrayList<bean>(); try { Connection conn = dbutil.getConn(); Statement state = null; String sql="select * from policy"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println("搜索全部政策"); while(rs.next()){ bean lu = new bean(); lu.setId(rs.getString("id")); lu.setType(rs.getString("type")); lu.setOrgan(rs.getString("organ")); lu.setPubdata(rs.getString("pubdata")); lu.setName(rs.getString("name")); list.add(lu); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { System.out.println("发生错误"); e.printStackTrace(); } return list; } public List<bean> search(String name,String document,String organ){ List<bean> list = new ArrayList<bean>(); try { Connection conn = dbutil.getConn(); Statement state = null; String sql = "select * from policy where name REGEXP '"+name+"' and document REGEXP '"+document+"' and organ REGEXP '"+organ+"'"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println("多项条件搜索运行中"); while(rs.next()){ bean lu = new bean(); lu.setId(rs.getString("id")); lu.setType(rs.getString("type")); lu.setOrgan(rs.getString("organ")); lu.setPubdata(rs.getString("pubdata")); lu.setName(rs.getString("name")); list.add(lu); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { System.out.println("发生错误"); e.printStackTrace(); } return list; } public static bean getOne(String id){ bean lu = new bean(); try { Connection conn = dbutil.getConn(); Statement state = null; String sql="select * from policy where id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,id); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ lu.setId(rs.getString("id")); lu.setType(rs.getString("type")); lu.setCategory(rs.getString("category")); lu.setOrgan(rs.getString("organ")); lu.setPubdata(rs.getString("pubdata")); lu.setName(rs.getString("name")); lu.setDocument(rs.getString("document")); lu.setRange(rs.getString("range")); lu.setPerdata(rs.getString("perdata")); lu.setViadata(rs.getString("viadata")); lu.setText(rs.getString("text")); } rs.close(); pstmt.close(); conn.close(); } catch(SQLException e) { e.printStackTrace(); } return lu; } }
package Bean; public class bean { private String id; private String name;// private String type;// private String category; private String range; private String document; private String organ;// private String viadata; private String pubdata;// private String perdata; Menu menu; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getRange() { return range; } public void setRange(String range) { this.range = range; } public String getDocument() { return document; } public void setDocument(String document) { this.document = document; } public String getOrgan() { return organ; } public void setOrgan(String organ) { this.organ = organ; } public String getViadata() { return viadata; } public void setViadata(String viadata) { this.viadata = viadata; } public String getPubdata() { return pubdata; } public void setPubdata(String pubdata) { this.pubdata = pubdata; } public String getPerdata() { return perdata; } public void setPerdata(String perdata) { this.perdata = perdata; } }
package Bean; import java.util.List; public class Menu { private Integer id; private String type; private List<Menu> bean; public Menu(Integer id,Integer pid,String type) { this.id =id; this.type= type; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "Menu{" + "id=" + id + ", type='" + type + '\'' + ", bean=" + bean + '}'; } public List<Menu> getBean(){ return bean; } public void setBean(List<Menu> bean) { this.bean = bean; } }
package DBUtil; import java.sql.*; public class dbutil { static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; //数据库驱动名 static final String url = "jdbc:mysql://localhost:3306/policy";//数据库地址 static final String user = "root"; static final String password = "123456"; //连接数据库 public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 public static void close (PreparedStatement preparedState, Connection conn) { if (preparedState != null) { try { preparedState.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (preparedState != null) { try { preparedState.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } //测试是否连接成功 public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement preparedStatement = null; ResultSet rs = null; String sql ="select * from policy limit 10";//数据库名称 preparedStatement = conn.prepareStatement(sql); rs = preparedStatement.executeQuery(); if(rs.next()){ System.out.println("数据库不为空"); } else{ System.out.println("数据库为空"); } } }标签:String,rs,冲刺,查询,lu,conn,close,public,五一 From: https://www.cnblogs.com/pinganxile/p/18250441