Link to home
Start Free TrialLog in
Avatar of Evan Redmond
Evan Redmond

asked on

java derby database query using user inputted text from the textfield

I have a java derby database, I can write to and read from the database.

I am having trouble:

Making it so that the text that the user enters into the text field, is then incorporated into the database query to determine the results displayed.

I TRIED it this way, the results were, if I click the search button, it will return the info/query into the "run" screen, **not** actually incorporating the user input into the query tho, I have to do that in the code, by replacing the abc to the number in the database.

Do I have to create some kind of command line argument? set the variable differently? Can I just replace the query info where the database info goes with a variable like how I tried in the upcoming example?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
            String abc = jTextField1.getText();
            String data = "jdbc:derby://localhost:1527/sample";
        try (
              Connection conn = DriverManager.getConnection(
              data, "app", "app");
              Statement st = conn.createStatement())   { 
          
          Class.forName("org.apache.derby.jdbc.ClientDriver");
          ResultSet rec = st.executeQuery(
                  "select ROW1, ROW2, ROW3, ROW4, ROW5 from APP.NAME1 "
                          + "where (ROW4 = 'abc')");
            while (rec.next())  {
              System.out.println("ROW1:\t"
              + rec.getString(1));
              System.out.println("ROW2:\t"  + rec.getString(2));
              System.out.println("ROW3:\t"  + rec.getString(3));
              System.out.println("ROW4:\t" + rec.getString(4));
              System.out.println("ROW5:\t"  + rec.getString(5));
              System.out.println();
          }
          st.close();
          
            } catch (SQLException s)  {
          System.out.println("SQL Error: " + s.toString()  + " "
                  + s.getErrorCode() + " " + s.getSQLState());
      } catch (Exception e) {
          System.out.println("Error: " + e.toString()
          + e.getMessage());
    }                                        
    }
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        
    }    

Open in new window

               

Thank you for the time!
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Evan Redmond
Evan Redmond

ASKER

It worked! I also made it so that it outputs the info to a few new textfields on the app. As far as displaying the database info for the user to see, I want to display text(either a few lines of text or a paragraph) and possibly an image. Do you happen to know the best swing component to use for that part of the app? text area?

Thanks for the help!
Yeah, you can use a JTextArea for the multiline text and there a number of ways to show images, but a JLabel can do it, or you can draw it onto a JPanel with a bit more code.

You're welcome!
@Evan,

You messaged and said you don't need any further assistance but did my posts help you achieve a working solution? It certainly sounded that way from your reply. If that's the case, the way to finalise the question is to accept the post(s) that helped as the "Accepted Solution" awarding the expert(s) that have helped with points. If you need help in doing this, you can use the Request Attention button near the top of this question.