aspose file tools*
The moose likes JDBC and the fly likes Not able to connect to Mysql through servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Not able to connect to Mysql through servlet" Watch "Not able to connect to Mysql through servlet" New topic
Author

Not able to connect to Mysql through servlet

Rupali Kulkarni
Greenhorn

Joined: Mar 13, 2012
Posts: 5
Hi,

I am getting following exception , when i am trying to connect to oracle database.


[b]HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: JDBC driver not found.
LoginServlet.service(LoginServlet.java:69)

root cause

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1438)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1284)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
LoginServlet.service(LoginServlet.java:49)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.35 logs.
[/b]








My code inside servlet is as below;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class LoginServlet extends GenericServlet {

@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Service Method");

PrintWriter pw=res.getWriter();

Connection con=null;
Statement stmt=null;
ResultSet rs=null;

try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection("jdbc:mysql://localhost:1521/","DBSNMP","Html123#");
stmt=con.createStatement();
rs=stmt.executeQuery("Select * from login");
while(rs.next())
{
pw.println(rs.getObject(1).toString());
pw.println("\t\t\t");
pw.println(rs.getObject(2).toString());
pw.println("<br>");
}
}

catch(SQLException e)
{
throw new ServletException("Servlet could not display records",e);
}

catch(ClassNotFoundException e)
{
throw new ServletException("JDBC driver not found.",e);
}

finally
{
try
{
if(rs!=null)
{
rs.close();
rs=null;
}
if(con!=null)
{
con.close();
con=null;
}
if(stmt!=null)
{
stmt.close();
stmt=null;
}
}

catch (SQLException e) { } // TODO: handle exception
}

pw.close();
}



/**
* Constructor of the object.
*/
public LoginServlet() {
super();
System.out.println("LoginServelet constructor");
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
System.out.println("Destroy Method");
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.println("Init Method");
}

}




Please let me know the solution for this problem, Thanks in advance.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Rupali Kulkarni wrote:Hi,

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");


Why are you using a MS SqlServr driver to talk to MySql?
Rupali Kulkarni
Greenhorn

Joined: Mar 13, 2012
Posts: 5
I am using


com.mysql.jdbc.Driver



But still i am not able to connect. Will you please let me know step by step procedure to do that ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

If you are going to handle database connections yourself instead of delegating that job to the servlet container, then you need to put the database driver's jar file into your web application. The correct place to put it is in the WEB-INF/lib folder.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Rupali Kulkarni wrote:I am using com.mysql.jdbc.Driver


Not according to the code you posted. And not according to the error message that that code produced, either.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Do you not even know what your own code is doing?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rupali Kulkarni
Greenhorn

Joined: Mar 13, 2012
Posts: 5
I know what my code is doing.

My code is as below;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class LoginServlet extends GenericServlet {

@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Service Method");

PrintWriter pw=res.getWriter();


Connection con=null;
Statement stmt=null;
ResultSet rs=null;

try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:1521/","DBSNMP","Html123#");
stmt=con.createStatement();
rs=stmt.executeQuery("Select * from login");
while(rs.next())
{
pw.println(rs.getObject(1).toString());
pw.println("\t\t\t");
pw.println(rs.getObject(2).toString());
pw.println("<br>");
}
}

catch(SQLException e)
{
throw new ServletException("Servlet could not display records",e);
}

catch(ClassNotFoundException e)
{
throw new ServletException("JDBC driver not found.",e);
}

finally
{
try
{
if(rs!=null)
{
rs.close();
rs=null;
}
if(con!=null)
{
con.close();
con=null;
}
if(stmt!=null)
{
stmt.close();
stmt=null;
}
}

catch (SQLException e) { } // TODO: handle exception
}

pw.close();
}



/**
* Constructor of the object.
*/
public LoginServlet() {
super();
System.out.println("LoginServelet constructor");
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
System.out.println("Destroy Method");
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.println("Init Method");
}

}


And when i am trying to run it, Using Apache tomcat Server 5.5.35 It is giving me following error. I just want to retrieve the values from my table in SQL and simply print those.


HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: JDBC driver not found.
LoginServlet.service(LoginServlet.java:69)

root cause

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1438)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1284)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
LoginServlet.service(LoginServlet.java:49)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.35 logs.




I want steps to connect to mySQL database, Do i need to specify value for CLASSPATH variable?

From where should i get that path? please give me the response including all the details.

Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Paul Clapham wrote:If you are going to handle database connections yourself instead of delegating that job to the servlet container, then you need to put the database driver's jar file into your web application. The correct place to put it is in the WEB-INF/lib folder.


I already gave you all the details. Those are all the details.

No, you don't have to set the classpath for your servlet container. Just deploy the database driver jar as part of your application as I described.

And do make sure you actually recompile your code to refer to the correct driver and deploy that recompiled code. Last time you didn't, because you got this error message:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Rupali Kulkarni wrote:[b]From where should i get that path? please give me the response including all the details.


People on this site are friendly, but we won't do your homework for you.

Your attitude is starting to bother me.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

Rupali Kulkarni wrote:I am using
com.mysql.jdbc.Driver

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Rupali Kulkarni wrote:I know what my code is doing.


These three statements cannot coexist.
Rupali Kulkarni
Greenhorn

Joined: Mar 13, 2012
Posts: 5
Which .Jar file do i need to use? where it's located?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Bear Bibeault wrote:
Rupali Kulkarni wrote:I am using
com.mysql.jdbc.Driver

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Rupali Kulkarni wrote:I know what my code is doing.


These three statements cannot coexist.


Especially when combined with this one:

Rupali Kulkarni wrote:I am getting following exception , when i am trying to connect to oracle database.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Rupali Kulkarni wrote:Which .Jar file do i need to use? where it's located?


Oh dear. Does this mean you haven't even downloaded the JDBC driver yet? If that's the case then I'm willing to bet that you aren't running a MySQL server on your computer. Is that right?

At any rate your first step is to decide which database you're actually going to use. So far you have asserted that you are using three different databases. There's no point in continuing until you are sure which one you plan to use.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Wow, this has three different Sql engines. Microsoft Sql Server, Oracle's MySql, and Oracle's main product: Oracle.

Each of which has separate and distinct JDBC drivers.
Rupali Kulkarni
Greenhorn

Joined: Mar 13, 2012
Posts: 5
Ok , Let me explain my problem in simplest way;

I want to connect to oracle database 11g, for this i am doing code in servlet's Service() method.
But it is giving me the error which i have mentioned before.

If it is related with .JAR file, then where is it located? where do i need to place that file. Please explain the same.

Thanks!
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

I suggest that you write a simple Java main class that can talk to the Oracle database using Oracle's JDBC driver. Remove servlets from the equation until you get that working.

As you get that to work, you will learn where to put the Oracle JDBC driver, and how to use it.

The code that you've blindly copy-and-pasted has references to MySql, that is a separate product and it needs a distinct driver.

Once that is working, its a fairly small step to servlets. But doing it all at once is a bad idea, as there are too many variables.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Okay. So it's Oracle?

In that case get rid of all the code which refers to MySQL and to SQL Server, and replace it by code which loads the Oracle driver and connects to the Oracle database. Make sure you know where the database is running -- do you have a database already running somewhere which you have to connect to, or do you have to run your own server instance? At any rate make sure you use the right computer name in your JDBC URL.

As for the jar file, you will need to get the Oracle JDBC driver jar which corresponds to the version of the Oracle database you plan to connect to. And where do you need to put that jar? I have told you that twice already. Please don't ask questions if you don't intend to read the answers you get.

And as for servlets: I suggest you follow Pat's advice and forget about servlets for now. There are plenty of things wrong with the servlet code you posted, but going into them would be a distraction. Just focus on JDBC and get that working.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Rupali, could you please UseCodeTags next time? Thanks.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to connect to Mysql through servlet