• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Small JDBC application.

 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am working on an web application, that uses JDBC to access MS SQL 2005 database. There are 4 to 5 queries, based on actions on the screens.

Is this a good design. For each query, I may be writing same code:




 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Class.forName call only needs to executed once per JVM lifetime, not for each and every JDBC call.
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it mean, I have to create a new connection string object for every action (or every query that is being executed during the same session) ?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you by connection string - a connection is not a string-like object.

No, you do not need a new connection for each query. But since you're working on a web app, you need to be concerned about concurrent threads. If you use a connection in a way so that it may be shared by multiple threads (maybe by storing it in a static variable, or as a context attribute), then you may run into concurrency issues.

For web apps it's common to address these issues by using a database connection pool from where you obtain and to where you return the connections.
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already set up a data source on the application server. Can you tell me how should I go about using the connection pool in simple JDBC.

Thanks

Srinivas
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends a lot on which server and DB you're using. Check the documentation for your server and/or DB. E.g., for Tomcat it's described here.
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already configured data source. My concern is the code. Can you give steps/sample code on how to utilize the data source to write JDBC code.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/datasource.html should get you started. Section 4.1.4 has some code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic