java derby database query, with user input text -
i have java derby database, can write , read database.
i having trouble:
making text user enters text field, incorporated database query determine results displayed.
i tried way, results were, if click search button, return info/query "run" screen, not incorporating user input query tho, have in code, replacing abc number in database.
do have create kind of command line argument? set variable differently? can replace query info database info goes variable how tried in 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 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) { }
you not setting variable correctly. instead of setting row4 'abc'
need set variable. try this.
"select row1, row2, row3, row4, row5 app.name1 " + "where (row4 = '"+abc+"')"
its better use preparedstatement. avoid lot of problem related sql injection.
string selectsql = "select row1, row2, row3, row4, row5 app.name1 row4 = ?"; preparedstatement preparedstatement = dbconnection.preparestatement(selectsql); preparedstatement.setstring(1, abc); resultset rs = preparedstatement.executequery(selectsql );
Comments
Post a Comment