SQL transaction processing can be executed in the stored procedure or in Java. The operation is simple and marked in red. The Code is as follows:
Import java. SQL .*;
Import javax. SQL .*;
Import javax. Naming .*;
Connection conn = NULL;
Statement stmt = NULL;
Resultset rs = NULL;
Try
{
// Connect to the database
Context CTX = new initialcontext ();
Datasource DS = (datasource) CTX. Lookup ("JDBC/test ");
// Connect to the database
Conn = Ds. getconnection ();
Stmt = conn. createstatement ();
Try
{
// The setting is not automatically submitted
Conn. setautocommit (false );
// Execute SQL
String SQL = "insert ....";
Int dbresult = stmt.exe cuteupdate (SQL );
// After the SQL statement is executed, submit the SQL statement.
Conn. Commit ();
Catch (exception E)
{
// Write back if an exception occurs
Conn. rollback ();
}
}
Catch (exception E)
{
Result =-1;
E. printstacktrace ();
}
Finally
{
If (RS! = NULL)
{
Try
{
Rs. Close ();
}
Catch (exception E)
{
}
}
If (stmt! = NULL)
{
Try
{
Stmt. Close ();
}
Catch (exception E)
{
}
}
If (Conn! = NULL)
{
Try
{
Conn. Close ();
}
Catch (exception E)
{
}
}
}
Q group: 236201801