0

I am new to the site and Java. Here's my issue: I am trying to get a simple query working, but nothing happens.

con = DriverManager.getConnection(host, uName, uPass);

        stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );

     } catch (SQLException ex) {

         JOptionPane.showMessageDialog(this,ex.getMessage());
     }
     try {
         String nome = ricercaetichetta.getText();
         rs = stmt.executeQuery("SELECT * From Giocatori Where Nome='" + nome + "'");
         while(rs.next()) {

             String nomeric = rs.getString("Nome");

             risultati.setText("\n"+nomeric);

}                                       

     }    catch (SQLException ex) {

         JOptionPane.showMessageDialog(this,ex.getMessage());
     }
}

I have a JTextField where you can write an input for WHERE clause. Then I'd like to write my results in JTextArea. When I run the program, it just lops without writing anything....

EDIT: After reading your comments, I tried this. It seems working, but is sensitive case....I'm going to look how to solve this. Thank you very much. If there are problems with this code, please tell me, I'm learning.

try {
        String SQL ="SELECT * From Giocatori Where Nome=?";
        PreparedStatement preparedStatement = con.prepareStatement(SQL);
        String nome= ricercaetichetta.getText();
        preparedStatement.setString(1, nome);
        System.out.println(nome); //try
        rs = preparedStatement.executeQuery();
        while (rs.next()) {
            String firstName = rs.getString("Nome");
            System.out.println(preparedStatement);
            risultati.append(firstName+"\n");
}                                       
     }
     catch (SQLException ex) {
         JOptionPane.showMessageDialog(this,ex.getMessage());
     }
}
CCC
  • 170
  • 1
  • 15
  • Are you able to debug or otherwise print out the value of nome before running the query to make sure it is correctly getting what you expect? Also, beware SQL injection here. Check out [this question](http://stackoverflow.com/questions/4333015/does-the-preparedstatement-avoid-sql-injection) for more on that. – mschor Mar 25 '16 at 20:23
  • How do you know the query is returning data? As a side note, you should be using [prepared statements](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) instead of concatenating the variables into the query. – Mick Mnemonic Mar 25 '16 at 20:26
  • [Using Prepared Statements](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) – MadProgrammer Mar 25 '16 at 21:12
  • Tried a new code....you can see the edit – CCC Mar 25 '16 at 23:59

0 Answers0