L
leni
Hello,
I'm a newbe and I've got a problem with my web application.
I wrote a simple servlet, which is supposed to connect with a
database, but each time I try to run it, the result is "Cannot load
JDBC driver class 'null'" exception. I dunno what is wrong.
I use Tomcat 5 and MySQL. the database name is "organizer" and test
aplication pth is tomcat..\webapps\baza.
My web.xml:
<?xml version="1.0" encoding="iso-8859-2" ?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<description>MySQL Test App</description>
- <resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/organizer</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
- <servlet>
<servlet-name>Start</servlet-name>
<description>Prezentacja</description>
<servlet-class>Start</servlet-class>
</servlet>
- <servlet-mapping>
<servlet-name>Start</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>
</web-app>
**************** Servlet code: ************************
public class Start extends HttpServlet {
Connection con;
private boolean conFree = true;
private String baza = "jdbc/organizer";
DataSource dataSource;
public void init() throws ServletException {
try {
Context init = new InitialContext();
Context contx = (Context) init.lookup("java:comp/env");
dataSource = (DataSource) contx.lookup(baza);
} catch (NamingException exc) {
throw new ServletException(
"Nie mogę uzyskać źródła", exc);
}
}
public void destroy() {
}
public void serviceRequest(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html; charset=windows-1250");
PrintWriter out = resp.getWriter();
out.println("<html><head><title>Test</title></head><body><h2>Lista
dostępnych uzytkownikow</h2>");
Connection con = null;
try {
synchronized (dataSource) {
con = dataSource.getConnection();
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users;");
out.println("<table border=1 cellpadding=2 cellspacing=2>");
while (rs.next()) {
String login = rs.getString("login");
String haslo = rs.getString("haslo");
String imie = rs.getString("imie");
String nazwisko = rs.getString("nazwisko");
String telefon = rs.getString("telefon");
String row = "<tr><td>Login: " + login + "</td><td>imie: " +
imie + "</td><td>nazwisko: " + nazwisko + "</td><td>numer telefonu: "
+ telefon + "</td></tr>";
out.println(row);
}
rs.close();
stmt.close();
out.println("</table></body></html>");
} catch (Exception exc) {
out.println(exc.getMessage());
} finally {
try { con.close(); } catch (Exception exc) {}
}
out.close();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
serviceRequest(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
serviceRequest(request, response);
}
}
******************* context element on server.xml file
*********************
<Context path="/baza" docBase="baza"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/organizer"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/organizer">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on
this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- Class name for the old mm.mysql JDBC driver - uncomment this
entry and comment next
if you want to use this driver - we recommend using
Connector/J though
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
-->
<!-- Class name for the official MySQL Connector/J driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that
the
mm.mysql JDBC Driver will automatically reconnect if mysqld
closed the
connection. mysqld by default closes idle connections after
8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/organizer</value>
</parameter>
</ResourceParams>
</Context>
Thank you in advance,
leni
I'm a newbe and I've got a problem with my web application.
I wrote a simple servlet, which is supposed to connect with a
database, but each time I try to run it, the result is "Cannot load
JDBC driver class 'null'" exception. I dunno what is wrong.
I use Tomcat 5 and MySQL. the database name is "organizer" and test
aplication pth is tomcat..\webapps\baza.
My web.xml:
<?xml version="1.0" encoding="iso-8859-2" ?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<description>MySQL Test App</description>
- <resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/organizer</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
- <servlet>
<servlet-name>Start</servlet-name>
<description>Prezentacja</description>
<servlet-class>Start</servlet-class>
</servlet>
- <servlet-mapping>
<servlet-name>Start</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>
</web-app>
**************** Servlet code: ************************
public class Start extends HttpServlet {
Connection con;
private boolean conFree = true;
private String baza = "jdbc/organizer";
DataSource dataSource;
public void init() throws ServletException {
try {
Context init = new InitialContext();
Context contx = (Context) init.lookup("java:comp/env");
dataSource = (DataSource) contx.lookup(baza);
} catch (NamingException exc) {
throw new ServletException(
"Nie mogę uzyskać źródła", exc);
}
}
public void destroy() {
}
public void serviceRequest(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html; charset=windows-1250");
PrintWriter out = resp.getWriter();
out.println("<html><head><title>Test</title></head><body><h2>Lista
dostępnych uzytkownikow</h2>");
Connection con = null;
try {
synchronized (dataSource) {
con = dataSource.getConnection();
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users;");
out.println("<table border=1 cellpadding=2 cellspacing=2>");
while (rs.next()) {
String login = rs.getString("login");
String haslo = rs.getString("haslo");
String imie = rs.getString("imie");
String nazwisko = rs.getString("nazwisko");
String telefon = rs.getString("telefon");
String row = "<tr><td>Login: " + login + "</td><td>imie: " +
imie + "</td><td>nazwisko: " + nazwisko + "</td><td>numer telefonu: "
+ telefon + "</td></tr>";
out.println(row);
}
rs.close();
stmt.close();
out.println("</table></body></html>");
} catch (Exception exc) {
out.println(exc.getMessage());
} finally {
try { con.close(); } catch (Exception exc) {}
}
out.close();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
serviceRequest(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
serviceRequest(request, response);
}
}
******************* context element on server.xml file
*********************
<Context path="/baza" docBase="baza"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/organizer"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/organizer">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on
this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- Class name for the old mm.mysql JDBC driver - uncomment this
entry and comment next
if you want to use this driver - we recommend using
Connector/J though
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
-->
<!-- Class name for the official MySQL Connector/J driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that
the
mm.mysql JDBC Driver will automatically reconnect if mysqld
closed the
connection. mysqld by default closes idle connections after
8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/organizer</value>
</parameter>
</ResourceParams>
</Context>
Thank you in advance,
leni