Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regarding insert max id in to databse

843840Mar 19 2009 — edited Mar 19 2009
Dear all,

In my project i used like this for inserting data,

step 1 : getting max id from the table

step 2 : using that max id inserting the data

public datacall{
AccessObj accesdataobj = new AccessObj();
public int access(String s,String s2,String s3)
{
int i=0;
try
{

// insert data in first table....
long loginid = accesdataobj.fmaxId("select max(userloginid) from login_tb");
String s31 = "insert into login_tb values(" loginid "," + s1 + ",'"+s2+"','" + s2 + "',null)
i =accesdataobj.insertUpdateDeleteRecord(s31);

//insert data in second table
long userlogid = accesdataobj.fmaxId("select max(userlogid) from userlog_tb");
String s31 = "insert into userlog_tb values(" +userlogid + "," + loginid+ ",'"+s2+"','" + s2 + "',null)
i =accesdataobj.insertUpdateDeleteRecord(s31);


}catch(Exception e){}
return i;
}
}

AccessObj.class


public long fmaxId(String s)
throws SQLException
{
long l = 0L;
Object obj = null;
try
{
createNewConnection();
stmt = super.cnewcon.createStatement();
ResultSet resultset = stmt.executeQuery(s);
if(resultset.next())
l = resultset.getLong(1);
l++;
}
catch(Exception exception)
{
System.out.println("SQL Exception ::" + exception.getMessage());
}
finally
{
try
{
closeConnection()
}
catch(Exception _ex) { }
}
return l;
}

public int insertUpdateDeleteRecord(String s)
throws SQLException
{
int i = 0;
try
{
createNewConnection();
stmt = super.cnewcon.createStatement();
i = stmt.executeUpdate(s);
}
catch(SQLException sqlexception)
{
System.out.println("Insert Update =" + sqlexception.getMessage());
}
finally
{
try
{
closeConnection();
}
catch(Exception _ex) { }
}
return i;
}



My question is ,

1. My idea for inserting data is correct?.

2. same time two users are inserting data in same time, that time i getting Primary key conflict error is coming in second table.

I need help in this.. inserting primary key, is there any procedure is there.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2009
Added on Mar 19 2009
2 comments
196 views