Java隱約查詢辦法詳解。本站提示廣大學習愛好者:(Java隱約查詢辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java隱約查詢辦法詳解正文
當我們須要開辟一個辦法用來查詢數據庫的時刻,常常會碰到如許一個成績:就是不曉得用戶究竟會輸出甚麼前提,那末怎樣樣處置sql語句能力讓我們開辟的辦法不論接收到甚麼樣的前提都可以正常任務呢?這時候where '1'='1'加上list便可以完善處理這個成績了,空話少說,上代碼:
// 隱約查詢辦法 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數據庫:
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<m.length();j++) //{ //if (hashtable.containsValue(Integer.parseInt(j)+"*") //hashtable.put(j, //} } } public void valueChanged(ListSelectionEvent event) { System.out.println(list.getSelectedIndex()); } }
經由過程這兩個實例年夜家能否對java隱約查詢辦法有了必定的懂得,願望年夜家愛好小編的文章,持續存眷哦!