How to run SQL TRANSACTION in a PreparedStatement - java

I have a SQL Transaction query that I am unable to run. Can any one tell me please why? I have failed to run it using preparedstament.executequery(); as well.
START TRANSACTION;
SELECT total_installment_remaining FROM payment_loan WHERE loan_id = 1 FOR UPDATE;
UPDATE payment_loan SET total_installment_remaining =total_installment_remaining-1 WHERE loan_id = 1;
COMMIT;

Turn off autocommit, then use Connection.commit() to end the transaction.
connection.setAutocommit(false);
Statement stmt = con2.createStatement();
// this will automatically start a transaction
ResultSet rs = stmt.executeQuery("SELECT total_installment_remaining FROM payment_loan WHERE loan_id = 1 FOR UPDATE");
// process the result if needed
...
stmt.executeUpdate("UPDATE payment_loan SET total_installment_remaining =total_installment_remaining-1 WHERE loan_id = 1");
// end the transaction and persist then changes
connection.commit();
If you don't need the result of the SELECT in your code, then you don't really need the SELECT ... FOR UPDATE in the first place, because the UPDATE will lock the row anyway.

String query = "START TRANSACTION;SELECT total_installment_remaining FROMpayment_loan WHERE loan_id = 1 FOR UPDATE;UPDATE payment_loan SET total_installment_remaining =total_installment_remaining-1 WHERE loan_id = 1;COMMIT;";
try {
ps2 = con2.prepareStatement(query);
ResultSet rs2 = ps2.executeQuery();
while (rs2.next()) {
rs2.getInt(1);
}
// rs.close();
// con.close();
} catch (SQLException e) {
logger.error("",e);
// TODO Auto-generated catch block
e.printStackTrace();
}

Related

Updating a value in a random row through java in a sql table

Image of my table as it is when I start this program.
What I have is a mostly empty table and I am trying to assign a value to a fixed number of elements. The column I am trying to edit is "Geschlecht" and the number of rows I want to edit is "copyMaen" (~50.000 entries). I would like to only select the rows where the value of "Geschlecht" was NULL before and I would like to select the rows randomly.
I am using SQLite through a JDBC driver.
This is the first time for me working with sql. This is how I tried to do it.
try {
Statement stmt = DBController.connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Geschlecht FROM individuen WHERE Geschlecht IS NULL;");
PreparedStatement ps = DBController.connection.prepareStatement("UPDATE individuen");
while (copyMaen != 0) {
if (rs.getRowId((int) (Math.random() * ReadCSV.sumBev)) == null) {
ps.setInt(2, 0);
ps.executeUpdate();
copyMaen--;
}
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
Obviosly this throws me Errors and I am not really sure how to go on from there. Could somebody point me in the right direction?
For anybody interested this is the solution:
try {
Statement stmt = DBController.connection.createStatement();
String select = "SELECT ID FROM individuen WHERE Geschlecht is NULL ORDER BY RANDOM()" +
" LIMIT " + Integer.toString(copyMaen);
ResultSet rs = stmt.executeQuery(select);
PreparedStatement ps = DBController.connection.prepareStatement("UPDATE individuen set Geschlecht = ? WHERE ID = ?;");
// rs.beforeFirst();
int count = 0;
while (rs.next()) {
ps.setInt(1, 0);
ps.setInt(2, rs.getInt(1));
ps.addBatch();
if (count%100==0) {
System.out.println(count);
}
count++;
}
DBController.connection.setAutoCommit(false);
ps.executeBatch();
DBController.connection.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
}
Try this,
Below update query is enough for update all row where Geschlecht is
null.
try {
Statement stmt = DBController.connection.createStatement();
String updateTableSQL = "UPDATE individuen set Geschlecht = ? where Geschlecht IS NULL";
PreparedStatement preparedStatement = dbConnection.prepareStatement(updateTableSQL);
preparedStatement.setString(1, "0"); // set zero where Geschlecht null found
// execute update SQL stetement
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}

How to prevent from transaction to close at the catch(Exception e)?

I'm trying to run a query from java to PosgresSQL and I get an error from the stmt.execute(sql)
I would like to execute a new query to help me print out the specific row failing, but when I get to the catch (Exception e) the transaction is aborted.
I cant create a new transaction because I'm working with temp tables. How do I prevent the transaction to abort?
org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
try (Statement stmt = data.db.getConnection().createStatement()) {
// data.db.getConnection().setSavepoint("sp01");
// insert to fact table
TableSchema factTableSchema = factInfo.getTableSchema();
// build SQL
String sql = "Select * From....";
try {
stmt.execute(sql); // this row is failing
}
catch (Exception e) {
try {
// now i would like to run a query only in case arrived here, but the transaction is closed
// how could i prevent from trasanction to close ?
ResultSet rs = stmt.executeQuery(" SELECT Bla,Bla From..");
Log.debug("");
}
catch (Exception e2) {
Log.debug("");
}
}
Create a new statement in the 2nd try block before you execute the query.
Statement stmt = data.db.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(" SELECT Bla,Bla From..");
ResultSet needs to be connected and alive to perform a next();

PreparedStatement to update one value based on another in a record

I have below table structure
id msg keyword
-----------------
1 abc ?
2 xyz ?
The above is just an example; however my real table looks like that only. Based on the value of msg field, I need to call an API that would calculate the keywords from the msg and then update the particular record. How can I get the record and update as well, at the same time using Java PreparedStatement?
Also since my database is very large, what would be the efficient way to do it? Below is the code snippet :
public void updateColumns() {
try {
PreparedStatement stmt = null;
ResultSet resultSet = null;
String query = "select * from '" + Constants.tableName + "'";
// How to uypdate the record here by calling my custom API that reads the msg and returns the keywords in the message??
stmt = conn.prepareStatement(query);
stmt.execute();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The idiomatic JDBC solution would be to generate a batch update :
String select = "SELECT id, msg FROM my_table";
String update = "UPDATE my_table SET keyword = ? WHERE id = ?";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(select);
PreparedStatement ps = conn.prepareStatement(update);) {
while (rs.next()) {
ps.setInt(1, rs.getInt(1));
ps.setString(2, MyAPI.calculateKeyword(rs.getString(2));
ps.addBatch();
}
ps.executeBatch();
}
Of course, if you table is very large, you may which to consider every X rows.

Not able to get count using JDBC connection

I'm not able to get the number of rows by making a query. I've used JDBC connection for it. But, rather it doesn't show any error, but it stops executing the process.
Here is my code
Transaction transaction=null;
Connection c = null;
Statement stmt = null;
ResultSet result;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection(
"jdbc:postgresql://192.168.1.7:5432/"+userDB,
"postgres", "openerp");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
int count = 0;
try {
LOGGER.info("In try of get");
factory = HibernateUtil.connectDB(userDB);
LOGGER.info("Connected to DB");
//Open Session
session = factory.openSession();
//Begin Transaction
transaction = session.beginTransaction();
LOGGER.info("Beginning the transaction");
stmt = c.createStatement();
String sql = "SELECT COUNT(*) FROM mycontent_messages where status = '0'";
count = stmt.executeUpdate(sql);
LOGGER.info("successfully got count : "+count);
c.commit();
stmt.close();
c.close();
Exception which I'm getting is
org.postgresql.util.PSQLException: A result was returned when none was expected.
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:307)
at net.vsspl.mycontent.daoimpl.contentImpl.getUnreadCount(contentImpl.java:379)
at net.vsspl.mycontent.services.countUnreadMessages.main(countUnreadMessages.java:49)
It logs upto "Beginning the transaction". After that it doesn't show anything. Is there any issue in my code?
You want to get the result set from the query and process it that way, not get the number of affected rows..
ResultSet rs = stmt.executeQuery(sql);
int count = 0;
if(rs.next())
{
count = rs.getInt(1);
}
count = stmt.executeUpdate(sql);
You're not executing an update; you're executing a query.
here you are executing a query to fetch the count of the records so you should be using
resultSet = stmt.executeQuery(sql);

Rollback does not work

I want to rollback all records have been inserted to table when exception occurs.
but conInsert.rollback() doesn't work.
Maybe I miss some code?
Here is my code
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connectionUrl);
//con.setAutoCommit(false);
Statement st = con.createStatement();
String querySelectOrderInTp = "SELECT order_in_tp_id, order_in_tp_qty, order_in_tp_price, order_in_tp_article_tc_id, order_in_tp_warehouse_tc_id, inv_stock_tp_id, inv_stock_tp_qty_available from order_in_tp LEFT JOIN inv_stock_tp on(order_in_tp_warehouse_tc_id=inv_stock_tp_whouse_current_id AND order_in_tp_article_tc_id=inv_stock_tp_article_tc_id AND order_in_tp_price=inv_stock_tp_price) where order_in_tp_pick_up_timestamp = 'A' AND order_in_tp_date = '2013-06-11' GROUP BY order_in_tp_id;";
ResultSet rs = st.executeQuery(querySelectOrderInTp);
String queryUpdateInvStockTp = "INSERT INTO inv_stock_tp (cre_tms,upd_tms,cre_usr,upd_usr,version,usr_act,inv_stock_tp_id,inv_stock_tp_key, inv_stock_tp_whouse_current_id,inv_stock_tp_article_tc_id,inv_stock_tp_qty_available,inv_stock_tp_qty_min,inv_stock_tp_price) VALUES (NOW(),NOW(),'demo2','demo2',1,'A',null,'AAAA',?,?,?,0,2000.0000)";
conInsert = DriverManager.getConnection(connectionUrl);
conInsert.setAutoCommit(false);
ps = conInsert.prepareStatement(queryUpdateInvStockTp);
String queryUpdateOrderInTp = "UPDATE order_in_tp set order_in_tp_pick_up_timestamp = ? WHERE order_in_tp_id = ?";
psUpdate = con.prepareStatement(queryUpdateOrderInTp);
while(rs.next()) {
Integer qty = rs.getInt(7) - rs.getInt(2);
ps.setString(1, rs.getString(5));
ps.setString(2, rs.getString(4));
ps.setString(3, rs.getString(2));
ps.execute();
psUpdate.setString(1, "A");
psUpdate.setString(2, rs.getString(1));
psUpdate.execute();
ps.clearParameters();
psUpdate.clearParameters();
}
conInsert.commit();
} catch (Exception e) {
e.printStackTrace();
if (conInsert != null) {
try {
System.err.print("Transaction is being rolled back");
conInsert.rollback();
} catch (SQLException excep) {
excep.printStackTrace();
}
}
}
I make an exception in last record but all record before it still have been inserted.
By default, MySQL runs with autocommit mode enabled. This means that as soon as you execute a statement that updates (modifies) a table, MySQL stores the update on disk to make it permanent. I dont know java, but make sure that You have the START TRANSACTION statement, and then COMMIT or ROLLBACK.

Categories