This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JDBC and the fly likes pls help me to put this into a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "pls help me to put this into a servlet" Watch "pls help me to put this into a servlet" New topic
Author

pls help me to put this into a servlet

david hongw wang
Greenhorn

Joined: Mar 28, 2006
Posts: 6
EnterpriseDB;JDBC;Servlet

ClassNotFound: com.edb.Driver

---------------------------------------------------------------------
I try to put the following code into a servlet:
----------------------------------------------------------------------
1:
the following code can be compiled and run when I put into a stand-alone .java
----------------------------------------------------------------------

import java.sql.*;

public class Test {
public static void main(String args[]){
try{
String testsql="SELECT * FROM testtable";
Class.forName("com.edb.Driver");
Connection con = DriverManager.getConnection(
"jdbc:edb://localhost:5444/testdb","enterprisedb","12345678");
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(testsql);

while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
rs.close();
statement.close();
con.close();
System.out.println("Command successfully executed");
}//try
catch(ClassNotFoundException e){
System.out.println("Class Not Found : " + e.getMessage());
}
catch(SQLException exp){
System.out.println("SQL Exception: " + exp.getMessage());
System.out.println("SQL State: " + exp.getSQLState());
System.out.println("Vendor Error: " + exp.getErrorCode());
}
}//main
}//Test

-------------------------------------------------------------------
2:
when I try to put the above code into a servlet (the servlet is compiled and run), I was told
"ClassNotFound: com.edb.Driver"
------------------------------------------------------------------

import java.sql.*;

import java.io.*;
import java.text.*;
import java.util.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class TestServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
String title ="Testing";
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");

String cust_id = request.getParameter("acc");
String cust_pwd = request.getParameter("pwd");

if ( cust_id.equals("") || cust_pwd.equals("") ){
out.println("Please log in with your Account and password");
}
else{


try{
String testsql="SELECT * FROM testtable";
Class.forName("com.edb.Driver");
Connection con = DriverManager.getConnection(
"jdbc:edb://localhost:5444/testdb","enterprisedb","12345678");
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(testsql);

while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
rs.close();
statement.close();
con.close();
System.out.println("Command successfully executed");
}//try
catch(ClassNotFoundException e){
System.out.println("Class Not Found : " + e.getMessage());
}
catch(SQLException exp){
System.out.println("SQL Exception: " + exp.getMessage());
System.out.println("SQL State: " + exp.getSQLState());
System.out.println("Vendor Error: " + exp.getErrorCode());
}


out.println("</body>");
out.println("</html>");
}//doGet

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}//doPost
}//TestServlet

------------------------------------------------------------------
Thank you very much!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Short answer: Try putting the Driver's JAR in the web-inf/lib directory for your servlet context.

Application servers do no use the classpath, this allows them to run multiple applications in a single VM as if they all had their own VM.
david hongw wang
Greenhorn

Joined: Mar 28, 2006
Posts: 6
it works!!!

thank you very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pls help me to put this into a servlet
 
Similar Threads
problem with mysql connecton!
Running a simple java program
sending data from servlet to bean
HTTP method POST is not supported by this URL
JDBC: NullPointerException