Why JavaBean load jdbc driver failed?


A

Allen

In jsp, forName("sun.jdbc.odbc.JdbcOdbcDriver") run successfully. When
used in JavaBean,
forName will fail. While run JavaBean class directly, it works. Why?

The following is the code.

package com;

import java.io.*;
import java.util.*;
import java.sql.*;

public class Publisher {

String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Connection conn = null;
ResultSet rs = null;

public Publisher() {
}

public boolean open(String url, String user, String passwd) {
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, user, passwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}

public ResultSet executeQuery(String sql) {

if (conn == null) {
return null;
}

rs = null;
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
return rs;
}

public void close() {

try {
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

Publisher pb = new Publisher();
pb.open("jdbc:eek:dbc:publisher", "user", "user");
ResultSet rs = pb.executeQuery("SELECT * FROM Publisher");
String id, name, url;
System.out.println("Publisher_ID Name URL");

try {
while (rs.next()) {
id = rs.getString("Publisher_ID");
name = rs.getString("name");
url = rs.getString("url");
System.out.println(id + " " + name + " " + url);
}
} catch (SQLException e) {
e.printStackTrace();
}

pb.close();
}
}

// !!!!!!!!!!!!!!!!!!!!!!!!! NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!
java com.Publisher // OK!

http://localhost:8080/publisher.jsp will report error for open(url,
user, passwd) return false;
 
Ad

Advertisements

A

Allen

// My platform is jre1.5.0_06, Tomcat 5.5
// JSP file

<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="pb" class="com.Publisher" scope="page" />
<html>
<head>
<title>List of Publisher</title>
</head>
<body>
<%!
String id, name, url;
%>
<h1>Publisher_ID Name URL</h1>
<% try {
if (!pb.open("jdbc:eek:dbc:publisher", "user", "user")) {
out.print("error");
}

ResultSet rs = pb.executeQuery("SELECT * FROM Publisher");
if (rs != null) {
while (rs.next()) {
id = rs.getString("Publisher_ID");
name = rs.getString("name");
url = rs.getString("url");
%>
<h2>
<%= id + " " + name + " " + url %>
</h2>
<% }
rs.close();
}
} catch (SQLException e) {
}
%>
</body>
</html>
 
A

Allen

java.sql.SQLException: [Microsoft][ODBC Driver Manager] not found data
source name and not spcified default driver
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.Publisher.open(Publisher.java:19)
at org.apache.jsp.publisher_jsp._jspService(publisher_jsp.java:65)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
 
Ad

Advertisements

A

Allen

Yes. I found it was caused by DriverManager.getConnection(url, user,
passwd).

When run java class directly, it can use odbc configured data source,
i.e.
database url can be "jdbc:eek:dbc:publisher".

But in JavaBean, url can only be used as
"jdbc:eek:dbc:driver={Microsoft Access Driver
(*.mdb)};DBQ=E:/publisher.mdb".

Why?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top