Java SQL Transaction Processing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.