This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes could not locate mysql driver using Javabean!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "could not locate mysql driver using Javabean!!!" Watch "could not locate mysql driver using Javabean!!!" New topic
Author

could not locate mysql driver using Javabean!!!

weixing wang
Greenhorn

Joined: Apr 28, 2003
Posts: 6
my configuration:win2000 tomcat4.1.24 jdk1.4.0
i have downloaded the mysql driver , put it to
the correct directory and set the CLASSPATH.
now the following java code can always run correctly .
-----------------dataLink.java-----------------------
public class dataLink
{
public static void main(String[] args){
Connection conn=null;
Statement stat=null;
ResultSet rst=null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("JDBC driver loaded.");
conn=DriverManager.getConnection("jdbc:mysql://localhost/address ?user=root&password=good");
stat=conn.createStatement();
rst=stat.executeQuery("select * from addressbook");
System.out.println("<table border=1>");
int i=1;
while (rst.next()){
System.out.println("<tr><td>");
System.out.println(rst.getString(1));
System.out.println("</td>");
System.out.println("<td>");
System.out.print(rst.getString(2));
System.out.println("</td>");
System.out.println("<td>");
System.out.print(rst.getString(3));
System.out.println("</td></tr>");
}
System.out.println("</table>");
System.out.println("Database connection established.");
} catch (ClassNotFoundException cnfe){
System.out.println("ClassNotFoundException:could not locate driver.");
} catch (SQLException cnfe){
System.out.println("SQLException:"+cnfe);
} catch (Exception e){
System.out.println("An unknown error occured while connecting to the database.");
}finally {
try{
if (rst!=null)
rst.close();
if (stat!=null)
stat.close();
if (conn!=null)
conn.close();
} catch (SQLException sqle) {
System.out.println("Unable to close database connection.");
}
}
}
};
------------------------------------------------------------------
but when i try to link to mysql using jsp and javabean,it failed;
the jsp and javabean code is following!!!
-------------------listgroupid.jsp-------------------------------
<%@ page language="java" import="java.sql.*,java.io.*,database.*,java.util.*" %>
<jsp:useBean id="usergroupList" class="database.bbs" />
<table>
<%
usergroupList.connect();
ResultSet rs=usergroupList.listUsergroup();
while (rs.next()){
%>
<tr>
<td><%=rs.getString("1")%></td>
<td><%=rs.getString("2")%></td>
</tr>
<%
}
%>
</table>
<%
usergroupList.disconnect();
%>
-------------------------------------------------------------------------

----------------------------bbs.java-------------------------------------
package database;
import java.sql.*;
import java.util.*;
public class bbs
{
String error;
Connection conn;
public bbs()
{
}
//连接数据库
public void connect() throws ClassNotFoundException,SQLException,Exception
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost/address ?user=root&password=good ");
}
catch (ClassNotFoundException cnfe)
{
error="ClassNotFoundException:Could not locate DB driver.";
throw new ClassNotFoundException(error);
}
catch (SQLException cnfe)
{
error="SQLException:Could not connect to database.";
throw new SQLException(error);
}
catch (Exception e)
{
error="Exception:An unknown error occurred while connecting to database.";
throw new Exception(error);
}
}
//关闭连接
public void disconnect() throws SQLException
{
try
{
if (conn!=null) {
conn.close();
}
}
catch (SQLException sqle)
{
error="SQLException:Unable to close the database connection";
throw new SQLException(error);
}
}
//打开记录集,并返回
public ResultSet listUsergroup() throws SQLException,Exception
{
ResultSet rst=null;
try
{
String queryString="select * from addressbook";
Statement stat=conn.createStatement();
rst=stat.executeQuery(queryString);
}
catch (SQLException sqle)
{
error="SQLException:Could not execute the query.";
throw new SQLException(error);
}
catch (Exception e)
{
error="An exception occured while retrieving usergroup.";
throw new SQLException(error);
}
return rst;
}
};
-------------------------------------------------------------------------
IE show me the following Exception note:
org.apache.jasper.JasperException: ClassNotFoundException:Could not locate DB driver.
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:536)

who can tell me why!!!
HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
Hi,
Do you include in the Tomcat classpath or System classpath?
You need to include it in the Tomcat classpath.
Alternatively, you can include the driver's jar in your web app's WEB-INF\lib\ directory.
Hope this helps.
Cheers.

Han Ming
weixing wang
Greenhorn

Joined: Apr 28, 2003
Posts: 6
thanks .i will have a try
weixing wang
Greenhorn

Joined: Apr 28, 2003
Posts: 6
may tell me the steps in detail?
weixing wang
Greenhorn

Joined: Apr 28, 2003
Posts: 6
i have solved the problem.
u must add "import org.gjt.mm.mysql.Driver;" in bbs.java
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: could not locate mysql driver using Javabean!!!
 
Similar Threads
mySQL insert pukes on me
JasperException with custom JSP tags
trouble in using connection pooling in tomcat
intermittent nullpointer using response.encodeURL error anyone know how to solve it
problem in getting datasource in tomcat???