aspose file tools*
The moose likes JDBC and the fly likes Small JDBC application. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Small JDBC application." Watch "Small JDBC application." New topic
Author

Small JDBC application.

forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
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
Marshal

Joined: Mar 22, 2005
Posts: 41501
    
  53
The Class.forName call only needs to executed once per JVM lifetime, not for each and every JDBC call.


Ping & DNS - my free Android networking tools app
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
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
Marshal

Joined: Mar 22, 2005
Posts: 41501
    
  53
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

Joined: Feb 24, 2009
Posts: 169
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
Marshal

Joined: Mar 22, 2005
Posts: 41501
    
  53
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

Joined: Feb 24, 2009
Posts: 169
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
Marshal

Joined: Mar 22, 2005
Posts: 41501
    
  53
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Small JDBC application.