• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet & JDBC

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the driver jar file in WEB-INF/lib directory.

MM
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please dont do Database manipulation stuff in your servlet.
thanks.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
reply
    Bookmark Topic Watch Topic
  • New Topic