首页 > 其他分享 >五一冲刺(政策查询系统)2

五一冲刺(政策查询系统)2

时间:2024-06-16 12:11:27浏览次数:9  
标签:String rs 冲刺 查询 lu conn close public 五一

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

相关文章

  • 政策查询系统(安卓)1
    老师现在又要求将此系统功能实现在安卓端,正好最近学习了springboot相关知识,便用springboot做后端,安卓的页面做前端编写了此系统创建springboot项目做好数据库配置spring:datasource:driver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/wu......
  • 政策查询系统(安卓)2
    controller层packagecom.leap.jixianceshiboot.controller;importcom.leap.jixianceshiboot.entity.Policy;importcom.leap.jixianceshiboot.entity.PolicyTypeCount;importcom.leap.jixianceshiboot.service.PolicyQueryService;importcom.leap.jixianceshiboot.util.P......
  • 5、Oracle的多表查询
    最近项目要用到Oracle,奈何之前没有使用过,所以在B站上面找了一个学习视频,用于记录学习过程以及自己的思考。视频链接:【尚硅谷】Oracle数据库全套教程,oracle从安装到实战应用如果有侵权,请联系删除,谢谢。本文主要讲解以下几点:使用等值和不等值连接在SELECT语句中查询多个表......
  • 蓝桥杯备考冲刺必刷题(C++) | 3791 珠宝的最大交替和
    学习C++从娃娃抓起!记录下蓝桥杯备考比赛学习过程中的题目,记录每一个瞬间。附上汇总贴:蓝桥杯备考冲刺必刷题(C++)|汇总-CSDN博客【题目描述】小莉是一位珠宝设计师,她非常喜欢玩珠子。她有一个长度为N......
  • 蓝桥杯备考冲刺必刷题(C++) | 3250 最大的卡牌价值
    学习C++从娃娃抓起!记录下蓝桥杯备考比赛学习过程中的题目,记录每一个瞬间。附上汇总贴:蓝桥杯备考冲刺必刷题(C++)|汇总-CSDN博客【题目描述】给定nnn副卡牌,每张卡牌具......
  • gbase8s的select查询数据物理位置寻址分析和学习
    1.执行的sql语句和目的执行的sql语句selectrowid,*fromtest1whereid=1;目的:查找到test1表中id=1这个值在硬盘中的真实位置并取出2.寻址的过程-确定表的数据空间和逻辑页的信息根据表名查看这张表的数据空间位置和逻辑页号[gbasedbt@iZ2ze5s78e4tanwe5q2znxZ~]$......
  • 第09章_子查询
    1.需求分析与问题解决1.1实际问题#查询工资大于Able的工资#方式1:内连接SELECTe1.last_name,e1.salaryFROMemployeese1,employeese2WHEREe1.`salary`>e2.`salary`ANDe2.`last_name`='Abel';#方式2:子查询SELECTlast_name,salaryFROMemployeesWHEREsa......
  • 快速提高MySQL查询效率的实用方法
    快速提高MySQL查询效率的实用方法包括以下几个方面,下面将详细列举并解释:使用合适的索引索引可以大大提高查询的速度,允许数据库系统快速定位和访问特定的数据行。在经常用于WHERE子句、JOIN操作和ORDERBY排序的列上创建索引。避免创建过多的索引,因为索引也会占用存储空间......
  • MySQL入门学习-子查询.ANY
        在MySQL数据库中,子查询是指一条查询语句嵌套在另一条查询语句中,可以用来实现复杂的查询逻辑。子查询通常在WHERE子句中使用,用于过滤或比较查询结果。    子查询ANY是指返回子查询结果集中的任意一个值,与其他子查询类型相比,子查询ANY的特点是返回值......
  • Java Web学习笔记55——DQL基本查询
    DQL:DQL英文全称DataQueryLanguage(数据查询语言),用来查询数据库表中的数据。关键字:SELECT。SELECT是最为常见,最为重要的操作。查询在一个系统中,能占据90%的以上。查询也是最为复杂的一个操作。 查询五子句。准备数据: --DQL-基本查询--1.查询指定字......