I'm a rookie in the java language, but i'm trying to create a search function for a mysql database. I've managed to get the search function working with a defined string, but i have no idea how a automatic generated jtextfield works or how they can connect it to an auto generated jbutton.
here is my code for the jtextfield and jbutton:
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// jTextField1.addActionListener(this);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.getText();
jComboBox1.getSelectedItem().toString();
try {
Connection con = DriverManager.getConnection("jdbc:ucanaccess://C:/Michel's Muziek Magazijn.accdb");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mmm WHERE "+jComboBox1+" = '"+jTextField1+"'");
while (rs.next()) {
System.out.println("\t" + rs.getString(2) + "\t" + rs.getString(3)+ "\t" + rs.getString(4));
}
rs.close();
} catch (SQLException e) {
System.out.println(e);
}
}
Like i said, i don't have any idea what to insert into the jtextfield or how to get the inserted data towards the jbutton.
I would be very grateful if anyone can help me, since an answer to this question can also completely finish my insert function for adding to the database.
Already thanks for reading,
Molnaris
You can redirect the text field action to the button using something like..
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt)
}
This means that when the user presses Enter, it will also trigger a search, which is a nice feature
You need to assign the values returned by the text field and combobox ...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String text = jTextField1.getText();
String combo =jComboBox1.getSelectedItem().toString();
try {
Connection con = DriverManager.getConnection("jdbc:ucanaccess://C:/Michel's Muziek Magazijn.accdb");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mmm WHERE "+combo+" = '"+text+"'");
while (rs.next()) {
System.out.println("\t" + rs.getString(2) + "\t" + rs.getString(3)+ "\t" + rs.getString(4));
}
rs.close();
} catch (SQLException e) {
System.out.println(e);
}
}
Having said that, I'd strongly encourage you to use a PreparedStatement
instead
PreparedStatement stmt = con.prepareStatement("SELECT * FROM mmm WHERE ? = ?");
stmt.setString(1, combo);
stmt.setString(2, text);
ResultSet rs = stmt.executeQuery();
See Using PreparedStatement
s for more details
You also not managing your resources very well, you should ensure that you are closing the various resources when you have finished with them...
try (Connection con = DriverManager.getConnection("jdbc:ucanaccess://C:/Michel's Muziek Magazijn.accdb")) {
try (PreparedStatement stmt = con.prepareStatement("SELECT * FROM mmm WHERE ? = ?")) {
try (ResultSet rs = stmt.executeQuery()) {
//...
}
}
} catch (SQLException e) {
e.printStackTrace();
}
Take a look at The try-with-resources Statement for more details