java - JDBC prepared statement select from table where -



java - JDBC prepared statement select from table where -

i trying write jdbc sqlite preparedstatement homecoming row customerid matches 1 passed in. method returning null pointer exception.

i not have great understanding of jdbc, sqlite or preparedstatements, can see have need , cant work out why not working. code below:

public static void customersselectwhere(jpanel customers, int custid) { customerstable(); string [] entries = new string[7]; connection c = null; preparedstatement pstmt = null; seek { class.forname("org.sqlite.jdbc"); c = drivermanager.getconnection("jdbc:sqlite:walkertechcars.db"); c.setautocommit(false); string query = "select * customers custid=? " ; resultset rs = pstmt.executequery( query ); pstmt = c.preparestatement(query); pstmt.setint(1, custid); pstmt.executeupdate(); c.commit(); while ( rs.next() ) { int custid = rs.getint("custid"); string phone = rs.getstring("phone"); string surname = rs.getstring("surname"); string firstname = rs.getstring("firstname"); string home = rs.getstring("home"); string address = rs.getstring("address"); string postcode = rs.getstring("postcode"); customers.add(customerstablesingle(integer.tostring(custid), firstname, surname, phone, home, address, postcode, false, customers )); } rs.close(); pstmt.close(); c.close(); } grab ( exception e ) { system.err.println( e.getclass().getname() + ": " + e.getmessage() ); system.exit(0); } } }

your syntax little off, first preaprestatement bind parameter , phone call executequery() like

string query = "select * customers custid=?"; // resultset rs = pstmt.executequery( query ); pstmt = c.preparestatement(query); pstmt.setint(1, custid); rs = pstmt.executequery(); // c.commit();

also, should close() in finally. so, like

public static void customersselectwhere(jpanel customers, int custid) { customerstable(); string[] entries = new string[7]; connection c = null; preparedstatement pstmt = null; resultset rs = null; seek { // should not need class.forname anymore. // class.forname("org.sqlite.jdbc"); c = drivermanager.getconnection("jdbc:sqlite:walkertechcars.db"); c.setautocommit(false); string query = "select * customers custid=?"; // resultset rs = pstmt.executequery( query ); pstmt = c.preparestatement(query); pstmt.setint(1, custid); rs = pstmt.executequery(); // c.commit(); while (rs.next()) { // create string begin with. // string custid = rs.getstring("custid"); string custid = integer.tostring(custid); string phone = rs.getstring("phone"); string surname = rs.getstring("surname"); string firstname = rs.getstring("firstname"); string home = rs.getstring("home"); string address = rs.getstring("address"); string postcode = rs.getstring("postcode"); customers.add(customerstablesingle(custid, firstname, surname, phone, home, address, postcode, false, customers)); } } grab (exception e) { system.err.println(e.getclass().getname() + ": " + e.getmessage()); } { if (rs != null) { seek { rs.close(); } grab (sqlexception e) { e.printstacktrace(); } } if (pstmt != null) { seek { pstmt.close(); } grab (sqlexception e) { e.printstacktrace(); } } if (c != null) { seek { c.close(); } grab (sqlexception e) { e.printstacktrace(); } } }

java database sqlite jdbc

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -