CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Java

 Inserting in multiple tables

Print topic Send  topic

Author Message
newbie
Posted: 09/19/2005, 8:31 AM

If I use "Before Insert" event and insert in the first table, and then insert to the 2nd table fails, I want to rollback the changes in the first table and remove the just inserted record.
How do I do this?

Also, can I just get the Connection that CCS uses, instead of degining a new conn object?

I am using the following code from CCS samples:

JDBCConnection conn = JDBCConnectionFactory.getJDBCConnection("ashishocg");
String sql = "INSERT INTO report (a) VALUES ('aaa')";
conn.executeUpdate(sql);
conn.closeConnection();
Richard Brown
Posted: 10/21/2005, 1:01 AM

I use this code below to do this:-

DBConnectionManager DBM;
Connection cn = null;
Statement stmt = null;
ResultSet rs = null;
int pk = 0;
Integer num = 0;

try {
DBM = DBConnectionManager.getInstance();
cn = DBM.getConnection("connection1");
cn.setAutoCommit(false);
stmt = cn.createStatement();
stmt.execute("update nextkey set id = id+1 where tablename='rep_composition'");
rs = stmt.executeQuery("select id from tablename where tablename='rep_composition'");

// Get new ID.
pk = Utils.convertToLong(rs.getInt("id")).intValue();
num = new Integer(pk);

// commit trans.
cn.commit();
} catch (Exception ex) {
try {
if (cn != null) {
cn.rollback();
}
e.getRecord().addError("Database error: " + ex.getMessage());
} catch (Exception ex2) {
e.getRecord().addError("Database error: " + ex2.getMessage());
}
} finally {
try {
if (rs != null) {
rs = null;
}
if (stmt != null) {
stmt = null;
}
if (cn != null) {
if (cn.isClosed() == false) {
cn.close();
}
cn = null;
}
} catch (Exception ex) {
e.getRecord().addError("Database error: " + ex.getMessage());
}
}

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.