Search code examples
javajdbcderby

Selecting a specific record from a Java Derby Database


How would I actually display a single record from a database with the record fields being displayed in related TextFields.
I've tried below but I select either of them and it only shows one record, where have I gone wrong?

String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver);
String host = "jdbc:derby://localhost:1527/NoteDatabase";
String uName = "Simon";
String uPass = "Password";

Connection con = getConnection(host, uName, uPass);
Statement stmt = con.createStatement();
stmt = con.createStatement();

stmt.executeQuery(" select * from " + lstGetItems.getSelectedValue() + "");
ResultSet rs = stmt.getResultSet();
if (rs.next()) {
    txtNoteTitle.setText(rs.getString("NOTENAME"));
    txtTextInput.setText(rs.getString(2));

Which gets me the below result. As in the screenshot

I tried this to fix but all I get is an exception when I click on the same note.

String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver);
String host = "jdbc:derby://localhost:1527/NoteDatabase";
String uName = "Simon";
String uPass = "Password";

Connection con = getConnection(host, uName, uPass);
Statement stmt = con.createStatement();
stmt = con.createStatement();

stmt.executeQuery(" select * from " + lstGetItems.getSelectedValue() + " WHERE 'NOTENAME' = " + lstGetNotes.getSelectedValue() + " ");
ResultSet rs = stmt.getResultSet();
if (rs.next()) {
    txtNoteTitle.setText(rs.getString("NOTENAME"));
    txtTextInput.setText(rs.getString("NOTECONENT"));

enter image description here


Solution

  • The syntax of Select is sql select:

    SELECT column1, column2, ... FROM table_name;
    

    Or

    SELECT * FROM table_name;
    

    So in your case if you want to select from a specific table (lstGetItems.getSelectedValue()) that is entered by the user then you have to check if your table name exist or not before you make your select.

    If you want to select from your table with WHERE something = somethingelse your second query is not correct because you miss to put your String between two '' and remove the two '' from the name of your column so instead you have to use:

    stmt.executeQuery(" select * from "+ lstGetItems.getSelectedValue() +" WHERE NOTENAME = '" + lstGetNotes.getSelectedValue() + "' " );
    

    I consider that lstGetItems.getSelectedValue() is your table name.
    and NOTENAME is your column name.
    and that lstGetNotes.getSelectedValue() the value of your column.

    But your way is not secure and can cause an error syntax of SQL Injection so instead you have to use PreparedStatement


    EDIT

    For example :

    try (PreparedStatement stmt = connection.prepareStatement(
            "select * from table_name WHERE NOTENAME = ?")) {
        stmt.setString(1, lstGetNotes.getSelectedValue());
    
        ResultSet result = stmt.executeQuery();
        if (result.next()) {
           ....
        }
    }