Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java servlet mysql connection with Tomcat

Posted on 2006-11-02
5
Medium Priority
?
543 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 200 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 1800 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 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