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 ?
views:
127answers:
3
+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
2009-12-02 09:33:47
I'm going to get just a row of data, always, this means I should set a fetch size of 1 ?
Omu
2009-12-05 08:21:41
+3
A:
- Use the MSSQL JDBC Driver to create a connection to the database
- 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
2009-12-02 09:39:20
The jTDS driver can be used to connect to MSSQL and Sybase.http://jtds.sourceforge.net/
crowne
2009-12-02 10:11:58