Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8390503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:00:28+00:00 2026-06-09T19:00:28+00:00

In my application, I am writing data to a database. This is the code

  • 0

In my application, I am writing data to a database.
This is the code for database writing.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class DatabaseRollback {

private static Connection getDBConnection() {

    Connection dbConnection = null;
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    System.out.println("PostgreSQL JDBC Driver Registered!");

    try {
    dbConnection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/MyDB","root","123");

        if (dbConnection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
        return dbConnection;
    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    return dbConnection;
}

public static void main(String args[]) throws SQLException {
    Connection dbConnection = null;
    PreparedStatement pstmt = null;

    String query = "insert into orders(orderid, execid, exectype, lastprice, lastqty, ordstatus) values(?, ?, ?, ?, ?, ?)";

        try {
            dbConnection = getDBConnection();
            dbConnection.setAutoCommit(false);

            pstmt = dbConnection.prepareStatement(query); // create a statement
            pstmt.setString(1, "OrderID"); 
            pstmt.setString(2, "Test02"); 
            pstmt.setString(3, "exectype");
            pstmt.setDouble(4, 100.00);
            pstmt.setInt(5, 100);  
            pstmt.setString(6,"ordstatus"); 

            pstmt.executeUpdate(); 

            dbConnection.commit();

            query.concat("error");
            dbConnection.prepareStatement(query);
            pstmt.setString(1, "eeeeeeeeeeeeeeeeeeeeeeee");
            pstmt.executeUpdate(); // here error comes.........
        } catch (Exception e) {
        //  e.printStackTrace();
            dbConnection.rollback();
        } 
     finally {

        if (pstmt != null) {
            pstmt.close();
        }

        if (dbConnection != null && !dbConnection.isClosed()) {
            dbConnection.close();
        }

    }
}}

After debug point reach to the dbConnection.commit(); I have checked database and one record was inserted. But then it goes through dbConnection.rollback(); section. but it doesn’t rollback inserted record. How can I implement rollback machanism?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:00:30+00:00Added an answer on June 9, 2026 at 7:00 pm
    dbConnection.commit();
    

    should be placed at the end of your transaction.

    pstmt.setString(1, "eeeeeeeeeeeeeeeeeeeeeeee");
     pstmt.executeUpdate(); // here error comes.........
     dbConnection.commit();
            } catch (Exception e) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an application that uses a SQL Server 2005 database. In the connection
I'm writing an application that uses a SQL Server 2005 database. In the connection
I'm writing an application which reads data from a Microsoft Access database and writes
I am writing data synchronization application between sql server and firebird. In firebird is
I'm writing an application that reads from a MySQL database and migrates data into
I am writing code to migrate data from our live Access database to a
My application is SL2 reading and writing data through an Entity Framework Model exposed
I am writing a Silverlight application that will be both reading and writing data
i m writing one application which encrypt and decrypt data using AES (ECB) mode.
I'm writing an application that needs to uncompress data compressed by another application (which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.