java - SQLite syntax error with JTable -


hi want to create jtable sqlite database. data should come sqlite database , stored in . new details added typing in text field . however, error message : "java.sql.sqlexception: near "(": syntax error"

what´s wrong?

    public class sqlite     {  public static defaulttablemodel model = new defaulttablemodel(); private static jtextfield fieldid; private static jtextfield fieldname; private static jtextfield fieldage; private static jtextfield fieldaddress; private static jtextfield fieldsalary; 

public static void main( string args[] ) {

connection c = null; statement stmt = null; try {   class.forname("org.sqlite.jdbc");   c = drivermanager.getconnection("jdbc:sqlite:test.db");   c.setautocommit(false);   system.out.println("opened database successfully");    stmt = c.createstatement();    c.commit();    jframe f = new jframe();   f.setlayout(new gridlayout(5,1));   f.setdefaultcloseoperation( jframe.exit_on_close );    fieldid = new jtextfield("id");   fieldname = new jtextfield("name");   fieldage = new jtextfield("age");   fieldaddress = new jtextfield("address");   fieldsalary = new jtextfield("salary");    string sql = "insert company (id,name,age,address,salary) " +           "values (fieldid.gettext(), feldname.gettext(), age.gettext(), feldaddress.gettext(), feldsalary.gettext()";   stmt.executeupdate(sql);    jtable table = new jtable(model);   f.add( new jscrollpane( table ) );    model.addcolumn("id");   model.addcolumn("name");   model.addcolumn("age");   model.addcolumn("address");   model.addcolumn("salary");    resultset rs = stmt.executequery( "select * company;" );   while ( rs.next() ) {      int id = rs.getint("id");      string  name = rs.getstring("name");      int age  = rs.getint("age");      string  address = rs.getstring("address");      float salary = rs.getfloat("salary");          model.addrow(new object[] {id, name, age, address, salary});   }    /**jtextfield feldid = new jtextfield("id");   jtextfield feldname = new jtextfield("name");   jtextfield feldage = new jtextfield("age");   jtextfield feldaddress = new jtextfield("address");   jtextfield feldsalary = new jtextfield("salary");     **/    f.pack();   f.setvisible( true );    rs.close();   stmt.close();   c.close(); } catch ( exception e ) {   system.err.println( e.getclass().getname() + ": " + e.getmessage() );   system.exit(0); } system.out.println("operation done successfully"); } } 

here problem :

"values (fieldid.gettext(), feldname.gettext(), age.gettext(), feldaddress.gettext(), feldsalary.gettext()"; 

they're read litterals , not reference getter.

here you'll have replace using concatenation.

"values ("+fieldid.gettext()+","+feldname.gettext()+","+age.gettext()+","+feldaddress.gettext()+","+feldsalary.gettext()+");";


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -