craftleft.gif (3002 bytes)QuickTable
Home | API | Tutorial | Download | Support | Contact topblack.gif (108 bytes)


QuickTable User Cp  |  Register  |  Members  |  Search  |  Help
    |- QuickTable Discussion Forums > JDBC/Database Issues Post New Topic   Post A Reply
inserting identity column printer friendly version
next newest post | next oldest post
Author Messages
ulvi
Unregistered
Edit or delete this message Reply w/Quote
Posted Wednesday, July 28, 2004 @ 20:27:43  

Hello,

I am using SQL-Server identity feature for primary key creating. I need to insert the new row to database for creating the primary key. Then I need to refresh() the table for getting key back for showing. But refresh() method takes the selected row to first line!. How can i code preventing this situtaion. I tried something with before and after insert methods but they don't work as another question.

Thanks and Best Regards

Admin
Board Owner

Gender: Unspecified
Location:
Registered: Jul 2003
Status: Offline
Posts: 9

Click here to see the profile for Admin Send email to Admin Send private message to Admin Find more posts by Admin Edit or delete this message Reply w/Quote
Posted Wednesday, July 28, 2004 @ 20:53:52  

The inserted row goes to the first row because that is the way the data comes back from database. You can make this row to go to the bottom by adding a "order by" to your sql query. If not, you can use DatabaseChangeListener class by implementing the onNewButtonClick() method.

class myListener extends DatabaseChangeListener
{

public boolean onNewButtonClick()
{
Connection conn = dbtable.getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate("insert..")
ResultSet rs = stmt.executeQuery("select identityColumn from ...") //select the newly inserted row
Integer identitiyVal = -1

if(rs.next())
{
identitiyVal = rs.getInteger(1);
}
else
{
//error
}

Vector insertVector = new Vector();

insertVector.addElement(identitiyVal);

insertVector.addElement("");
insertVector.addElement("");
...
insertVector.addElement("");

try{
dBTable.insert(insertVector);
}
catch(Exception e)
{
e.printStackTrace()
}

//since we have already inserted the record, return false, so that
//quicktable doesn't insert the default record
return false;
}

public boolean beforeInsert(int row)
{
//since we have already inserted this record into database
//we don't want to insert this record into database
return false;
}

}

buzzard
Unregistered
Edit or delete this message Reply w/Quote
Posted Friday, October 28, 2011 @ 04:49:59  

I needed to do the same thing, but I found it much simpler to add a DatabaseChangeListener and override the afterInsert() method.

Then, I simply call "select @@IDENTITY" on the table's connection, and then call table.setValueAt(id, row, 0) to apply the new identity value to the table.

Something like:

Connection cx = table.getConnection();
Statement st = cx.createStatement();
ResultSet rs = st.executeQuery( "select @@IDENTITY" );
if ( rs.next() )
{
table.setValueAt( rs.getObject( 1 ), row, 0 );
}

Post New Topic   Post A Reply Jump to:
Contact Us | QuickTable - A Java DBGrid | Privacy Policy All times are GMT -5 Hours.
Welcome to QuickTable Forums, Guest!  
Login
Username :
Password :
In order to fully utilize the abilities of this board, you are required to register as a member. Registration is free, and allows you to do lots of things including turning on or off certain features of this board. Register now!
Powered by CuteCast v2.0 BETA 2
Copyright © 2001-2003 ArtsCore Studios