how to execute both select & insert query in same place

Hi,

I want to select the data from table before going to insert the data into the same table.

But I am not able to insert the date after execute the select query.

Please anyone help me. ..how can I use both select and insert query in same java file.

pstmt = conn.prepareStatement ("select curr_date from telreport");

rs=pstmt.executeQuery();

String pre_date ="";

while(rs.next())

{

pre_date = rs.getString(1);

}

System.out.println("pre_date "+pre_date);

pstmt = conn.prepareStatement ("insert into Telreport (calldate ) values (sysdate);

pstmt.executeUpdate();

if (conn !=null )

{

pstmt.close();

}

}

Here i have used the one connection for select the data from table and again i have used insert statement I don抰 know wheatear its possible

It says the Error Like " maximum open cursors exceeded"

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:180)

Please help to to exectue bothe queries

Thanks

Merlina Rosin

[1407 byte] By [MerlinRosinaa] at [2007-11-26 17:45:44]
# 1

try this.

Connection connection = null;

Statement stmt = null;

ResultSet rs = null;

try {

connection = ... // Get connection from datasource or whatever

String pre_date = "";

stmt = connection.createStatement(

ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

rs = stmt.executeQuery("select curr_date from telreport");

if (rs.next()) {

pre_date = rs.getString(1);

}

System.out.println("pre_date " + pre_date);

if (stmt != null)

stmt.close();

if (rs != null)

rs.close();

stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,

ResultSet.CONCUR_UPDATABLE);

int updatedRows = stmt.executeUpdate("insert into Telreport (calldate ) values (sysdate)");

if (updatedRows <= 0)

throw new SQLException("INSERT failed to update any rows.");

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (stmt != null)

stmt.close();

if (connection != null)

connection.close();

} catch (SQLException e) {

stmt = null;

connection = null;

}

}

Seems as if Oracle is not allowing you to have more than 1 open cursor. On your code, you could try simply closing the statement after the first select to force the resources to be released.

simonkent1a at 2007-7-9 0:13:54 > top of Java-index,Java Essentials,Java Programming...
# 2
@Op. Note that you always should close all resultsets and statements. The code that you posted isn't closing the first statement so you are leaking resources.Kaj
kajbja at 2007-7-9 0:13:54 > top of Java-index,Java Essentials,Java Programming...
# 3
The above is good advise which should always be followed, but the statement will be closed by the driver when it becomes out of context. It's just this is no longer under your control and is unpredictable.
simonkent1a at 2007-7-9 0:13:54 > top of Java-index,Java Essentials,Java Programming...
# 4

> The above is good advise which should always be

> followed, but the statement will be closed by the

> driver when it becomes out of context.

I wouldn't rely on that. I have seen the error message mentioned above just because applications weren't closing rs and statements.

Kaj

kajbja at 2007-7-9 0:13:54 > top of Java-index,Java Essentials,Java Programming...
# 5
No, neither would I. This will happen, but it is unreliable and quite possible to get errors before it has. It's just that the leak is never permanent, that's all.
simonkent1a at 2007-7-9 0:13:54 > top of Java-index,Java Essentials,Java Programming...