Solved

java servlet mysql connection with Tomcat

Posted on 2006-11-02
5
509 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

831 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