views:

127

answers:

3

I have a MSSQL Database, and I have a stored procedure for any possible query, most of them just return a row of data with 3 columns or just execute an INSERT
How in java to connect to the DB and execute a stored procedure, and retrieve some data ?

+2  A: 

By reading and working through a JDBC Tutorial.

Bombe
+2  A: 

A connection pool like DBCP makes a big difference. The connection time can be save this way.

Prepared statements can help the database to skip query parsing. The parsed statements will be cached.

Batch updates help when you're executing a statement repeatedly.

Setting the right fetch size is another optimization for queries.

Thomas Jung
I'm going to get just a row of data, always, this means I should set a fetch size of 1 ?
Omu
+3  A: 
  1. Use the MSSQL JDBC Driver to create a connection to the database
  2. In jdbc, you need to create a CallableStatement to execute the procedure. It's like this:

.

CallableStatement callable = null;
try {
   String sqlCommand = "{call yourProcNameHere (?, ? /* ... */)}";
   callable = conn.prepareCall(sqlCommand);
   // ...
}
catch (SQLException e) {
   // ...
}
finally {
   / ...
}
Andreas_D
The jTDS driver can be used to connect to MSSQL and Sybase.http://jtds.sourceforge.net/
crowne