Solved

java servlet mysql connection with Tomcat

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

12 Experts available now in Live!

Get 1:1 Help Now