5.23

天完成了科技政策一点通的部分查询功能实现(在安卓端实现)

代码量:300

遇到困难:

sql语句的模糊查询的书写遇到了问题,不知如何去查询,

一般情况下:查询所有和条件查询使用两个方法(两个sql语句)实现

解决方法:分开写,分开调用

dao层语句

复制代码
 1 package com.lian.policy.Dao;
 2 
 3 import com.lian.policy.bean.Policy;
 4 import com.lian.policy.utils.DbOpenHelper;
 5 
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 public class PolicyDao extends DbOpenHelper {
10     //type是政策类型,range是政策实行的范围
11     public List<Policy> selectPoilcy(String type,String range,String text)
12     {
13 
14         List<Policy> list=new ArrayList<>();
15         try {
16             getConnection();
17             String sql="select * from policy WHERE name LIKE '%" + type + "%'";
18             pstmt=conn.prepareStatement(sql);
19             //pstmt.setString(1,type);
20 //            if(type!=null){
21 //                sql+="and type like '%" + type + "%'";
22 //            }
23 //
24 //            if(range!=null){
25 //                sql+="and range like '%" + range + "%'";
26 //            }
27 //            if(text!=null){
28 //                sql+="and text like '%" + text + "%'";
29 //            }
30             rs=pstmt.executeQuery();
31             while(rs.next()){
32                 Policy policy=new Policy();
33                 policy.setRange(rs.getString("range"));
34                 policy.setText(rs.getString("text"));
35                 policy.setType(rs.getString("type"));
36                 policy.setName(rs.getString("name"));
37                 policy.setId(rs.getLong("id"));
38                 list.add(policy);
39             }
40         }catch (Exception e){
41             e.printStackTrace();
42         }finally {
43             closeAll();
44         }
45         return list;
46     }
47 
48 
49 
50 
51     public List<Policy> selectAll()
52     {
53 
54         List<Policy> list=new ArrayList<>();
55         try {
56             getConnection();
57 
58             String sql="select * from policy";
59             pstmt=conn.prepareStatement(sql);
60             //pstmt.setString(1,type);
61 //            if(type!=null){
62 //                sql+="and type like '%" + type + "%'";
63 //            }
64 //
65 //            if(range!=null){
66 //                sql+="and range like '%" + range + "%'";
67 //            }
68 //            if(text!=null){
69 //                sql+="and text like '%" + text + "%'";
70 //            }
71             rs=pstmt.executeQuery();
72             while(rs.next()){
73                 Policy policy=new Policy();
74                 policy.setRange(rs.getString("range"));
75                 policy.setText(rs.getString("text"));
76                 policy.setType(rs.getString("type"));
77                 policy.setName(rs.getString("name"));
78                 policy.setId(rs.getLong("id"));
79                 list.add(policy);
80             }
81         }catch (Exception e){
82             e.printStackTrace();
83         }finally {
84             closeAll();
85         }
86         return list;
87     }
88 }
posted @ 2024-05-23 21:26  徐星凯  阅读(3)  评论(0编辑  收藏  举报