?
Solved

java servlet mysql connection with Tomcat

Posted on 2006-11-02
5
Medium Priority
?
532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month7 days, 22 hours left to enroll

765 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