Solved

java servlet mysql connection with Tomcat

Posted on 2006-11-02
5
506 Views
Last Modified: 2013-11-24
Hey guys,

It's been quite a while since i have used servlets so I'm hoping you guys can help me figure out what i'm missing.  What i'm trying to achieve is to simply connect to a mysql database and below is what i've done thus far.

1. created the servlet that initializes a DataServer Object
    DataServer conn = new DataServer();
2. created the DataServer class with an init method
    static Connection connect = null;

    //----should establish a connection to the database
    public static void init() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://ip:port/name";
            connect = DriverManager.getConnection(url,"username","password");
            connect.setAutoCommit(false);
        }
        catch (Exception ex) {
            System.out.println("Connection to the database failed: " + ex);
        }    
    }  
3.  i placed the mysqlconnectorj.jar and servlet.jar files into the WEB-INF/lib folder on tomcat
4. placed my classes in the WEB-INF/classes folder on tomcat
5. created an xml file to set name and path for the servlet
6. created an html file to call the servlet when a button is clicked within a form

Here is what seems to be happening, but it's seem so difficult to debug a servlet for some reason:
when i click the button it calls the servlet fine, but i check to see if the connection = null and simply print a page saying no connection or good connection.  Every time it comes back null.

At first I thought it would be the ip:port or username and password since it's a website so i pulled up mysql administrator on my machine and connected just fine remotely.

Any ideas?  All help is greatly appreciated.
0
Comment
Question by:tbboyett
  • 2
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17863825
check your log for errors

>     static Connection connect = null;

why static?

>   public static void init() {

where do u call this from?
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 50 total points
ID: 17863842
Why don't you use DataSource in Tomcat? It is the recommended way to connect to database and I think that it is efficient (as far as I know) as they have their own algorithms.

However, if you want to use the current codes, that is also fine.
0
 
LVL 5

Author Comment

by:tbboyett
ID: 17863848
hey objects, thanks for the response.
>> why static?
mistake in typing

>> where do u call this from?
would the init method not be called when i do DataServer conn = new DataServer(); from the servlet?
0
 
LVL 92

Accepted Solution

by:
objects earned 450 total points
ID: 17863881
> would the init method not be called when i do DataServer conn = new DataServer(); from the servlet?

no. you need to use a static block for that

static
{
   init();
}
0
 
LVL 5

Author Comment

by:tbboyett
ID: 17863888
ok thanks for the help, servlet rookie mistake :D

i changed the init to a constructor and it's works perfectly.

public DataServer() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://ip:port/name";
            connect = DriverManager.getConnection(url,"user","pass");
            connect.setAutoCommit(false);
        }
        catch (Exception ex) {
            System.out.println("Connection to the database failed: " + ex);
        }    
    }  

thanks again for your patience
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now