-1

I am implementing a search method, i want to search a data in a Jtable ( wich contains 2 columns id and name) based in id and name both. Till now I can search using just one of, id or name but cannot do that using them both. I tried a solution, but it is not working it just search for the last one ( id or name). For example if i start with try and catch by name and then id, it only goes with id search. And if I start with id and the name, it searches just by name. Can u help me please.

The code :

`private void textField1KeyReleased(java.awt.event.KeyEvent evt) {                                       

    PreparedStatement pst=null;
    ResultSet rs=null;
     if (textField1.getText().length() > 0){


     try{
    String sql = "select * from compte_utilisateur where nom=?";
     pst=maConnexion.ObtenirConnexion().prepareStatement(sql);
     pst.setString(1, textField1.getText());   
     rs=pst.executeQuery();
    TableUtilisateur.setModel(DbUtils.resultSetToTableModel(rs));}

    **catch(Exception e){JOptionPane.showMessageDialog(null, e);} 
     try{
    String sql = "select * from compte_utilisateur where id_utilisateur=?";
     pst=maConnexion.ObtenirConnexion().prepareStatement(sql);
     pst.setString(1, textField1.getText());   
     rs=pst.executeQuery();
    TableUtilisateur.setModel(DbUtils.resultSetToTableModel(rs));}

    catch(Exception e){JOptionPane.showMessageDialog(null, e);} 

     }

     else update_table();

}`**       
user3862042
  • 21
  • 1
  • 4
  • 8
  • 1
    To populate your table based on text fields values see [this related question and answer](http://stackoverflow.com/questions/25100297/how-to-populate-all-items-in-jtable-when-corresponding-jtextfield-is-empty/). – dic19 Aug 05 '14 at 10:44
  • It is not working, can u give me other solutions please – user3862042 Aug 05 '14 at 11:25
  • *"It's not working"* is a totally useless statement, such as "The sky is blue" or "The Sun is bright" are. What exactly do you mean "not working"? What did you try and didn't work, actually? Did you even read that post? Do you you have problems to build your SQL query? Where do you get stuck? – dic19 Aug 05 '14 at 11:34
  • I've error in that line : private Map parameters = new HashMap<>(); – user3862042 Aug 05 '14 at 11:38
  • It's working now, I did : `select * from compte_utilisateur where nom=? or id_utilisateur=?` and I added `pst.setString(1, textField1.getText()); pst.setString(2, textField1.getText()); pst.setString(3, textField1.getText());` Thank u very much for ur help – user3862042 Aug 05 '14 at 11:46

2 Answers2

0

Use just one SQL call passing the same value from the field twice

select * from compte_utilisateur where nom=? or id_utilisateur=?
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I already did but i've this error: java.sql.SQLException: No value specified for parameter 2 – user3862042 Aug 05 '14 at 11:42
  • It's working now, I did : `select * from compte_utilisateur where nom=? or id_utilisateur=?` and I added `pst.setString(1, textField1.getText()); pst.setString(2, textField1.getText()); pst.setString(3, textField1.getText());` Thank u for ur help – user3862042 Aug 05 '14 at 11:48
0

You should to add jar/folder this rs2xml.jar in your project

String sql="Select * From Inventarizimi where Regjistrimi=?"; Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con= (Connection) DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","Inventarizimi"); PreparedStatement preStatement = con.prepareStatement(sql); preStatement.setString(1, txtRegjistrimi.getText()); ResultSet result = preStatement.executeQuery(); table.setModel(DbUtils.resultSetToTableModel(result));