Problem appending row to database
Hi there,
I've read all the tutorials and I'm having a problem appending a row to the database, using a Table Component.
I have created two buttons, "Add" and "Save". When I click "Add", a new row appears at the bottom of the table, with a new ID value correctly set. When I click "Save", the new row disappears.
Here is my add method:
public String add_action() {
try {
RowKey rk = personnel_phoneDataProvider.appendRow();
personnel_phoneDataProvider.setCursorRow(rk);
Connection conn = null ;
Statement sqlStatement = null ;
ResultSet rs = null ;
javax.naming.Context ctx = new javax.naming.InitialContext() ;
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/phonelist") ;
conn = ds.getConnection() ;
// setup the connection
conn.setAutoCommit(true) ;
// execute the query
sqlStatement = conn.createStatement() ;
rs = sqlStatement.executeQuery("select PERSONNEL_PHONE_SEQ.NEXTVAL from dual" ) ;
rs.next() ;
int nextvalue = rs.getInt(1) ;
rs.close();
sqlStatement.close();
conn.close();
personnel_phoneDataProvider.setValue("ID", new BigDecimal(nextvalue));
} catch (Exception ex) {
log("Error Description", ex);
error(ex.getMessage());
}
return null;
}
Here is my "Save" method:
public String save_action() {
try {
personnel_phoneDataProvider.commitChanges();
personnel_phoneDataProvider.refresh();
} catch (Exception ex) {
log("Error Description", ex);
error("Error :"+ex.getMessage());
}
return null;
}
fyi, my database is Oracle, I've read up on all the problems with JSC and Oracle and I believe I've taken account of this - I do not think my problem is due me using Oracle.
Updates work fine.
Any help appreciated, I'm incredibly frustrated by this, I'm using JSC because it's supposed to be a productivity tool but I've wasted more time on this one problem than it would have taken me to hand code the whole thing in Netbeans.
Thanks
Richard
[2157 byte] By [
r.bremner] at [2007-11-26 8:41:54]

# 1
I haven't user oracle with jsc, just mysql but presume the principle is the same, I modify the dataprovider rather than using a datasource and result sets.
This works for me, hope it helps
Gaz
RowKey rk = usersDataProvider.appendRow() ;
usersDataProvider.setCursorRow(rk) ;
// Navigate through rows with data provider
usersDataProvider.setValue("users.username",(String)username.getValue());
usersDataProvider.setValue("users.password",(String)password.getValue());
usersDataProvider.setValue("users.firstname",(String)firstname.getValue());
usersDataProvider.setValue("users.lastname",(String)lastname.getValue());
usersDataProvider.setValue("users.email",(String)email.getValue());
usersDataProvider.setValue("users.notes",(String)notes.getValue());
usersDataProvider.setValue("users.accesslevel",(String)accessLevelRadioButton.g etValue());
usersDataProvider.commitChanges();
usersDataProvider.refresh();
# 3
Hi Richard,
1. Are you sure that you fill other columns with values, not only ID? You must fill all NOT NULL fields.
2. What error did you get? You can use Message Group component or Messsage component bound to your table to display error messages.
3. Actually you can use plain SQL to add a row, simply call
sqlStatement.executeUpdate("INSERT INTO my_table ..." ) ;
If you'll fail to find problem, go this way. Byt I'm sure that points 1 and 2 help you to identify problem.
Thanks, Misha
(Creator team)
# 9
Hi again, thanks for the help so far!
Still having the problem, seems insurmountable...
If I set the Id explicitly it makes no difference because I know the Id is getting a value form debugging it.
I already checked the INSERT statement, it looks fine other than it's too early in the process so I see ? instead of actual parameter values.
If I commit the data provider at the point of appending the row then it seems to work. that's no use though, I need to append the row when I click add, then save it when I click save.
Richard