Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet & JDBC

 
Siddhartha Mukherjee
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to connect a MySQL database using the following code in a standalone application, which was successful:

import java.sql.*;
public class testDB {
public static void main(String arg[]) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///website","root","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from InternalConfHall");

while(rs.next())
{String s = rs.getString(1);
System.out.println(s);
}
}
catch(Exception e) {}
}
}
//------------------------------------------------
But the same code of the data base connection block when used in a servlet code it has thrown an exception com.mysql.jdbc.Driver. So please help me to connect to the same mysql database using servlet code.The code I'v used is given below :

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class seeAvailability extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///website","root","");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from InternalConfHall");

while(rs.next())
{String s = rs.getString(1);
System.out.println(s);
}
}
catch(Exception e) {
out.println("Error:" + e.getMessage());
}

DO GET WILL INTERNALLY CALL DO POST METHOD
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doPost(req,res); //CALL DO POST METHOD, DEFINED ABOVE
}
}
//--------------------------------------------------------------------
Please help me
Thanks In Advance
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to put your driver where your app can find it.
Check your server's documentation to see how this is done.
 
Murasoli Maran
Ranch Hand
Posts: 193
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the driver jar file in WEB-INF/lib directory.

MM
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please dont do Database manipulation stuff in your servlet.
thanks.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For experiments it's fine but indeed for real life systems you should consider abstracting away the database access code to a separate set of classes (and using connection pooling).
 
prad gop
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put your corresponding jar file containing the driver in the WEB-INF/lib and try with
Class.forName("com.mysql.jdbc.Driver").newInstance();
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic