AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Prepared statement in Batch mode

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
Amir
Guest





PostPosted: Tue Jul 08, 2003 1:34 pm    Post subject: Prepared statement in Batch mode Reply with quote



Hi *

I need help before I go nuts. I've been using the batch mode
quite often for insert/updates. However I now have the case where I
need to execute multiple selects on the database. So I planned to put
them in a batch for evident performance reasons (instead of having a
loop)

Basically I have a table (my_table) with a varchar column called my_nb
I also have a list on numbers (there could be more that 300 of them)
and I need to know for each number if it's already in the table.

I created a little test class to see how I could deal with the problem
with a simple hard codded example:


import java.sql.* ;

public class DBTest {

public static void main(String[] args) {

System.out.println("Start test") ;

try {

Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
Connection conn = DriverManager.getConnection
("jdbc:sybase:Tds:***:***","myUser","myPassword");

String sql = "select count(*) from my_table where my_nb =
?";

PreparedStatement stmt = conn.prepareStatement(sql) ;

stmt.setString(1, "3000") ;
stmt.addBatch() ;

stmt.setString(1, "3001") ;
stmt.addBatch() ;

stmt.execute() ;

ResultSet rs = null ;
do {
rs = stmt.getResultSet() ;
System.out.println("ResultSet") ;
while(rs.next()) {
System.out.println(rs.getString(1)) ;
}
} while(stmt.getMoreResults()) ;

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

stmt.close() ;
conn.close() ;
rs = null ;
stmt = null ;
conn = null ;

} catch (Exception ex) {
ex.printStackTrace() ;
}

System.out.println("Stop test") ;
}
}


Now when I execute this It looks as if only the last sql in the batch
returns a result so instead of getting two result sets I only get one.

Even worse when I try using executeBatch() instead of execute() I get
a ClassCastException from the JDBC driver

Does anyone know how to make things work properly ?
Back to top
Daniel Dittmar
Guest





PostPosted: Tue Jul 08, 2003 6:14 pm    Post subject: Re: Prepared statement in Batch mode Reply with quote



Amir wrote:
Quote:
Hi *

I need help before I go nuts. I've been using the batch mode
quite often for insert/updates. However I now have the case where I
need to execute multiple selects on the database. So I planned to put
them in a batch for evident performance reasons (instead of having a
loop)
[...]

Now when I execute this It looks as if only the last sql in the batch
returns a result so instead of getting two result sets I only get one.

Even worse when I try using executeBatch() instead of execute() I get
a ClassCastException from the JDBC driver

Does anyone know how to make things work properly ?

It could simply be that Sybase does not support batch execution of
SELECTs. Some databases will create one ResultSet with the UNION, others
will create several ResultSets and others still will simply fail.

It could also be that it's not a limitation of Sybase, but rather of the
JDBC driver. It is definitivly not portable across databases.

Daniel


Back to top
Andree Große
Guest





PostPosted: Thu Jul 10, 2003 10:30 am    Post subject: Re: Prepared statement in Batch mode Reply with quote



Amir wrote:
Quote:
Hi *

I need help before I go nuts. I've been using the batch mode
quite often for insert/updates. However I now have the case where I
need to execute multiple selects on the database. So I planned to put
them in a batch for evident performance reasons (instead of having a
loop)

Basically I have a table (my_table) with a varchar column called my_nb
I also have a list on numbers (there could be more that 300 of them)
and I need to know for each number if it's already in the table.

I created a little test class to see how I could deal with the problem
with a simple hard codded example:

import java.sql.* ;

public class DBTest {
public static void main(String[] args) {
System.out.println("Start test") ;
try {
Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
Connection conn = DriverManager.getConnection
("jdbc:sybase:Tds:***:***","myUser","myPassword");

String sql = "select count(*) from my_table where my_nb =
?";

PreparedStatement stmt = conn.prepareStatement(sql) ;
...


Do not create a PreparedStatement for one SQL-Statement in this case.
If you want to use batch updates create only Statement-Object
and add SQL-Strings as follows:

Statement stmt = con.createStatement();
stmt.addBatch("select * from table where field=value1");
stmt.addBatch("select * from table where field=value2");
//--> etc.
stmt.executeBatch();
//--> get ResultSets from stmt-Objekt

Or you wonna do some other. You want to process the same
Statement with different parameters. So uses PreparedStatements
as follows (only in principle):

ResultSet rs = null;
PreparedStatement stmt =
con.prepareStatement("select * from table where filed = ?");

for (Iterator iter = values.iterator(); iter.hasNext(); ) {
String value = iter.next().toString();
stmt.setString(1, value);
rs = stmt.executeQuery();
read(rs);
rs.close();
}

HTH A.G.




Back to top
lordy
Guest





PostPosted: Thu Jul 10, 2003 10:25 pm    Post subject: Re: Prepared statement in Batch mode Reply with quote

[email]ajavanshir (AT) hotmail (DOT) com[/email] (Amir) wrote in news:189931e0.0307080534.6cb0ccf9
@posting.google.com:

Quote:

I need help before I go nuts. I've been using the batch mode
quite often for insert/updates. However I now have the case where I
need to execute multiple selects on the database. So I planned to put
them in a batch for evident performance reasons (instead of having a
loop)

Basically I have a table (my_table) with a varchar column called my_nb
I also have a list on numbers (there could be more that 300 of them)
and I need to know for each number if it's already in the table.


Not sure how portable it is across dbs but in Oracle I think this is
something like what you want????

select my_nb,count(*) from my_table where my_nb in (?,?,?,....,?) group by
my_nb;

You just build up you list of values '(?,?,?...?)' in advance?

Lordy

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.