java - Database Update & Sortable JTable Issues -


i've written custom jtable show contents of sqlite database in java program. here's code:

public class sortablejtable extends jtable{  public sortablejtable() {     super(); }  public sortablejtable(defaulttablemodel dtm) {     super(dtm); }  @override public void setvalueat(object obj, int row, int col) {     super.setvalueat(obj, row, col);     prgview.dbc.updateorderswithticketrequestid(this.getcolumnname(col).tostring(), obj, (integer)this.getvalueat(row, 0)); }  @override public class getcolumnclass(int c)  {     switch (c)     {         case 0:             return integer.class;         case 1:             return integer.class;         case 2:             return string.class;         case 3:             return string.class;         case 4:             return double.class;         case 5:             return double.class;         default:             return string.class;     } } } 

as can see, i'd whenever edits cell in custom jtable (which when setvalueat method called) able modify database contents reflect changes.

here's database code called within method.

public void updateorderswithticketrequestid(string columnname, object columnvalue, integer ticketrequestid) {     try     {         string sql = "update orders set ? = ? ticket_request_id = ?";          pstmt = conn.preparestatement(sql);          pstmt.setstring(1, columnname);         pstmt.setobject(2, columnvalue);         pstmt.setint(3, ticketrequestid);          pstmt.executeupdate();         pstmt.close();     }     catch (sqlexception e){} } 

once code gets statement:

 pstmt = conn.preparestatement(sql); 

in above code, returns error: sql error or missing database (near "?": syntax error).

i have 2 questions:

1) why getting error message? (am doing prepared statement incorrectly?)

2) getcolumnclass in custom jtable class not sorting columns correctly. sorted in string order, rather using switch cases have laid out. doing wrong?

thanks help!

1) why getting error message? (am doing prepared statement incorrectly?)

i don't know sql i'll make couple of suggestions:

  1. catch (sqlexception e){} - print out sqlexception.
  2. also, if not sure preparedstatement correct. try invoke sql using hardcoded values in sql string. once hardcode sql working can convert preparedstatement.

the getcolumnclass in custom jtable class not sorting columns correctly

the comparator used sorter based on getcolumnclass() method of tablemodel, not jtable.

so need override getcolumnclass(...) method of tablemodel. override setvalueat(...) method in tablemodel keep overrides in 1 class.

edit:

it possible can't specify column name variable. have used code in past successfully:

string sql = "update page set title = ? name = ?";  preparedstatement stmt = connection.preparestatement(sql);  stmt.setstring( 1, "update" ); stmt.setstring( 2, "name3" ); stmt.executeupdate(); stmt.close(); 

again, try hardcoded values first.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -