Omu
Omu

Reputation: 71188

fastest way to query database in java

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 ?

Upvotes: 0

Views: 3685

Answers (3)

Thomas Jung
Thomas Jung

Reputation: 33092

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.

Upvotes: 4

Andreas Dolk
Andreas Dolk

Reputation: 114757

  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 {
   / ...
}

Upvotes: 3

Bombe
Bombe

Reputation: 83846

By reading and working through a JDBC Tutorial.

Upvotes: 2

Related Questions