当我们需要开发一个方法用来查询数据库的时候,往往会遇到这样一个问题:就是不知道用户到底会输入什么条件,那么怎么样处理sql语句才能让我们开发的方法不管接受到什么样的条件都可以正常工作呢?这时where '1'='1'加上list就可以完美解决这个问题了,废话少说,上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
// 模糊查询方法
public List<person> query() {
List<person> list = new ArrayList<>();
Connection con = null ;
Scanner sc = new Scanner(System.in);
System.err.println( "enter name:" );
String name = sc.nextLine();
System.err.println( "enter id:" );
String id = sc.nextLine();
System.err.println( "enter tel:" );
String tel = sc.nextLine();
System.err.println( "enter sex:" );
String sex = sc.nextLine();
String sql = "select id,name,tel,sex from students "
// 技巧在此,合理拼接字符串
+ "where 1=1" ;
List<Object> list1 = new ArrayList<Object>();
//使用 commons-lang包
if (StringUtils.isNotEmpty(name)) {
sql += " and title like ?" ;
list1.add( "%" + name + "%" );
}
if (!StringUtils.isEmpty(id)) {
sql += " and content like ?" ;
list1.add( "%" + id + "%" );
}
if (!StringUtils.isEmpty(tel)) {
sql += " and addr like ?" ;
list1.add( "%" + tel + "%" );
}
try {
con = DSUtlis.getConnection();
// SQL语句组成完成以后,就生成pst对象
PreparedStatement pst = con.prepareStatement(sql);
// 设置?的值
for ( int i = 0 ; i < list1.size(); i++) {
pst.setObject(i + 1 , list.get(i));
}
ResultSet rs = pst.executeQuery();
while (rs.next()) {
person p = new person();
p.setId(rs.getString( "id" ));
p.setName(rs.getString( "name" ));
p.setTel(rs.getString( "tel" ));
p.setSex(rs.getString( "sex" ).equals( "1" ) ? "男" : "女" );
list.add(p);
}
rs.close();
pst.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
|
注解:
1、以上代码操作一个Oracle数据库:
1
2
3
4
5
6
7
|
create table students(
id varchar( 32 ),
name varchar( 30 ),
tel varcher( 15 ),
sex char ( 1 ),
constraint stud_pk primary key(id)
);
|
2、使用工具类获取Connection
3、proson是一个javabean
下面教大家如何用Java做模糊查询结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import java.util.*;
import java.util.regex.*;
//模糊查询
public class Media
{
public static void main( String args[])
{
JFrame frame= new MediaFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible( true );
}
}
class MediaFrame extends JFrame implements ActionListener,ListSelectionListener
{
private JList list;
private DefaultListModel m;
private JButton btn;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JFileChooser chooser;
private JTextField textField;
Map hashtable= new Hashtable();
private int i= 0 ;
int s= 0 ;
public MediaFrame()
{
setTitle( "Media" );
setSize( 600 , 500 );
JMenuBar menu= new JMenuBar();
setJMenuBar(menu);
JLabel label= new JLabel( "查询的歌曲名:" );
textField= new JTextField();
menu.add(label);
menu.add(textField);
JToolBar TB= new JToolBar();
m= new DefaultListModel();
list= new JList(m);
list.setFixedCellWidth( 100 );
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.addListSelectionListener( this );
JScrollPane pane= new JScrollPane(list);
chooser= new JFileChooser();
btn= new JButton( "添加歌曲" );
btn.addActionListener( this );
btn1= new JButton( "删除歌曲" );
btn1.addActionListener( this );
btn2= new JButton( "清空列表" );
btn2.addActionListener( this );
btn3= new JButton( "查找曲目" );
btn3.addActionListener( this );
btn4= new JButton( "排序" );
btn4.addActionListener( this );
JPanel panel= new JPanel();
panel.setLayout( new GridLayout( 5 , 1 ));
panel.add(btn);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
TB.setLayout( new GridLayout( 1 , 2 ));
TB.add(pane);
TB.add(panel);
add(TB,BorderLayout.WEST);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==btn)
{
i++;
chooser.setCurrentDirectory( new File( "." ));
int result=chooser.showOpenDialog(MediaFrame. this );
if (result==JFileChooser.APPROVE_OPTION)
{
System.out.println(i);
String name=chooser.getSelectedFile().getPath();
String str1=name;
int str2=name.lastIndexOf( "//" );
String name1=name.substring(str2+ 1 ,str1.length());
//截取最后一个"/"之前的所有字符串
int str3=name1.lastIndexOf( "." );
String name2=name1.substring( 0 ,str3);
//截取"."后面所有字符串后缀
hashtable.put(i,name2);
m.add( 0 ,hashtable.get(i));
System.out.println(hashtable);
}
}
if (event.getSource()==btn1)
{
m.removeElement(list.getSelectedValue());
System.out.println(m);
}
if (event.getSource()==btn2)
{
System.out.println(m);
i= 0 ;
hashtable.clear();
m.clear();
}
if (event.getSource()==btn3)
{
int [] a= new int[m.getSize()];
try
{
int j;
String name=textField.getText();
System.out.println(m.getSize());
for (j= 1 ;j<=m.getSize();j++)
{
Pattern p=Pattern.compile( "^" +name+ "+" ); //正则表达式选取以你填的单词为首的所有查询结果
Matcher match=p.matcher(( String )hashtable.get(j));
if (match.find())
{
s++;
//记录索引结点到数组中a[]中
a[s]=a[s]+m.getSize()-j;
System.out.println(hashtable.get(j));
System.out.println(a[s]);
}
}
//可以选择不多个选项(因为前面设置了JList可以多项选择)
list.setSelectedIndices(a);
}
catch (Exception e)
{
}
}
if (event.getSource()==btn4)
{
//int j;
//for (j=0;j<m.length();j++)
//{
//if (hashtable.containsValue(Integer.parseInt(j)+"*")
//hashtable.put(j,
//}
}
}
public void valueChanged(ListSelectionEvent event)
{
System.out.println(list.getSelectedIndex());
}
}
|
通过这两个实例大家是否对java模糊查询方法有了一定的了解,希望大家喜欢小编的文章,继续关注哦!