当我们需要开发一个方法用来查询数据库的时候,往往会遇到这样一个问题:就是不知道用户到底会输入什么条件,那么怎么样处理sql语句才能让我们开发的方法不管接受到什么样的条件都可以正常工作呢?这时where '1'='1'加上list就可以完美解决这个问题了,废话少说,上代码:
// 模糊查询方法 public Listquery() { List 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
注解:
1、以上代码操作一个Oracle数据库:
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做模糊查询结果
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
通过这两个实例大家是否对java模糊查询方法有了一定的了解,希望大家喜欢小编的文章,继续关注哦!



