Advertisement

05.08.2008 at 08:35AM PDT, ID: 23386466
[x]
Attachment Details

Data connection for servlets

Asked by tbboyett in Java Programming Language, Java Server Pages (JSP), J2EE

I am creating a site using JSP, tomcat, and mysql.  I don't want to have to add the same code to each page to connect to the database.  So, I am creating a class called DataConnection (the code so far is below).  

What I'm trying to figure out are the below questions:
1.  If more than one users are doing something that accesses this class at the same time, will the connections be unique to each user, or do i need to do anything special to make this happen.

2.  Does the code look correct for what I am trying to accomplish over the web.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
public class DataConnector {
   private Connection connect = null;
	
   public DataConnector() throws Exception {
      try {
         Class.forName("com.mysql.jdbc.Driver");
         connect = DriverManager.getConnection("myhost", "myusername", "mypassword");
         connect.setAutoCommit(false);
      }
      catch(Exception e) {
	 throw e;
      }
   }
	
   private Statement getStatement() {
      Statement s = null;
 
      while (s == null) {
         try {
	    s = connect.createStatement();
	 } catch (Exception e) {
	    try {
		wait(200);
	    } catch (Exception xx) {}
         }
      }
      return (s);
   }
	
   public void closeConnection() throws Exception {
      try {
         connect.close(); 
      }
      catch(Exception ex) {
         throw ex;
      }
   }
}
[+][-]05.08.2008 at 08:38AM PDT, ID: 21525660

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, Java Server Pages (JSP), J2EE
Sign Up Now!
Solution Provided By: CEHJ
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.08.2008 at 08:42AM PDT, ID: 21525693

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628