Using transaction with JSP
I am using JConnector of MYSQL 5.0 to connect the database.
Following are the codes of two insert statements.
public int insert(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/db?" +
"user=myname&password=12345");
String sSQL = "INSERT INTO tbl";
sSQL += "(fielda,fieldb)";
sSQL += " VALUES (?,?)";
prepStat = conn.prepareStatement(sSQL);
prepStat.setString(1,"a");
prepStat.setLong(2,"b");
prepStat.executeUpdate();
String sSQL = "INSERT INTO tblB";
sSQL += "(fielda,fieldb)";
sSQL += " VALUES (?,?)";
prepStat = conn.prepareStatement(sSQL);
prepStat.setString(1,"c");
prepStat.setLong(2,"d");
prepStat.executeUpdate();
String sSQL ="SELECT MAX(tblid) FROM tblB";
prepStat = conn.prepareStatement(sSQL);
rs=prepStat.executeQuery();
rs.next();
maxId= rs.getLong(1);
return maxId;
}
What I want to do is,
1)The transaction will be rollback if any one of the operation failed;
2)In tblB, there is a field called "tblid", which is an autoincrement field. I want return the value of "tblid" of the newly inserted field to the user if operation succeeded, or return -1 if operation failed.
But how to do this?

