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());
}
}