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();

b0wm0re at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 2
Thanks for the suggestion, but the only reason I'm using the DataSource is to get the next value for the ID column - that's is just an Oracle specific workaround, so don't let it distract you from the actual problem, which I still need help with... :-)Richard
rbremner at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 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)

Mikhail_Matveev at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 4
Check out this simple scenario using Oracle sequence to append a row: http://blogs.sun.com/roller/page/sakthi?entry=a_scenario_with_oracle_sequenceHTH,Sakthi
sakthivelgopal at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 5

Hi Mikhail,

1. Yes I'm sure all the other columns were given values.

2. I don't get an error, the appended row is just simply no longer part of the data provider (ok RowSet) when it comes to commit changes. yes canAppendRow == true

3. Thanks, I know all about using plain sql, trust me :-) I "want" to do it this way, because that's what the tool is for, otherwise I'd just use Netbeans.

Thanks, Richard

rbremner at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 6
Already read that, as you can see from the code I posted. Thanks anyway. Richard
rbremner at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 7

Hi Richard,

Now I have no reasonable idea what's the problem. Try to simplify your code step by step and check if it works. For example, use

personnel_phoneDataProvider.setValue("ID", new BigDecimal(1));

instead of block which creates connection, executed SQL and fetches necessary value. And so on. Will simpliest case work?

Also create sample application "Single Page CRUD with Table" and check if it works for you. It has similar functionality.

Thanks, Misha

(Creator team)

Mikhail_Matveev at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 8
Select the rowset in outline, check printStatements property .Re-run the project and check SunAppServer8/domains/creator/logs/server.log for the INSERT statement generated. It could possibly give you some clue.HTH,Sakthi
sakthivelgopal at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 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

rbremner at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...
# 10
If it is possible for you to share this piece of the project and schema, I could debug this case. Email this project/schem zipfile to CreatorFeedback At sun DOT com.Regards,Sakthi
sakthivelgopal at 2007-7-6 22:20:38 > top of Java-index,Development Tools,Java Tools...