在bean中DriverManager.getConnection与在jsp中用同样的驱动,为什么jsp可以而在bean中用的则出错呢?请各位帮忙(50分)

  • 主题发起人 主题发起人 DT
  • 开始时间 开始时间
D

DT

Unregistered / Unconfirmed
GUEST, unregistred user!
Fri Nov 14 01:31:00 CST 2003 : DriverClass: com.mysql.jdbc.Driver
Fri Nov 14 01:31:00 CST 2003 : Connection failed! URL: jdbc:mysql://localhost/eshop?user=username&password=passwd123456 Driver: com.mysql.jdbc.Driver User: esdata Pwd: eshop123456 No suitable driver
这是我的错误信息!
这是在jsp中调用一个bean进行DriverManager.getConnection,保存下来的Exception.getMessage()
而如果在直接在页面中用DriverManager.getConnection刚不会出错。
他们用了同样的Class.forName("com.mysql.jdbc.Driver").newInstance();
为什么,真是搞不懂,请大家帮忙指点一下。
 
你要抛出一个异常,或者捕捉这个异常才可以!
 
不会的把 是不是你的代码写的有问题,把你的代码贴出来
 
No suitable driver 是 catch(Exception e)捕获的e.getMessage()存下来的错误提示!
我的代码基本上是这样的:
在jsp中直接getConnection:
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection con = DriverManager.getConnection("jdbc:mysql://localhost/eshop?user=username&amp;password=passwd123456");
%>
正常无错.
而在bean中为这样:
public setDriver(String s)
{
try
{
Class.forName(s).newInstance();
}
catch(Exception ex)
{
//record error log
}
}
public void getConnection(String url)
{
try
{
con = DriverManager.getConnection(url);
//因为用的是最新版的mysql驱动,只要包含用户名和密码的url
//con为bean内的全局变量为connection
}
catch(Exception ex)
{
writeErrLog(ex.getMessage());
}
}
在jsp中调用该bean:
<jsp:userbean id="getCon" ..../>
<%
getCon.setDriver("com.mysql.jdbc.Driver");
getCon.getConnection("jdbc:mysql://localhost/eshop?user=username&amp;password=passwd123456");
%>
该jsp执行后就出错了,而上面那个直接在jsp中用的则没有问题。
 
全部代码如下:
bean:
package myeshopbean;
import java.io.*;
import java.util.*;
import java.beans.*;
import java.sql.*;
import javax.servlet.http.*;
import myeshopbean.AppConstants;
import myutils.*;
public class ConnectionManager implements AppConstants
{
/**The debug boolean =default is false */
private boolean debug = false;
protected Connection con;
protected String driver;
protected String passwd;
protected String url;
protected String user;
protected DebugWriter writer;

PropertyChangeSupport pcs;
public ConnectionManager()
{
pcs = new PropertyChangeSupport(this);
writer = new DebugWriter();
writer.setDebugFileName("c://debug.log");
}
public void setDriverClass(String s)
{
driver = s;
writer.writeDebug( "DriverClass: " + s );
try
{
Class.forName( driver ).newInstance();
}
catch(Exception ex)
{
pcs.firePropertyChange(driverClass, null, ex);
if (debug)
{
writer.writeDebug("Error setting driver: " + ex);
}
}
}
public void setConnectionUrl(String s)
{
url = s;
}
public void setDebug(String b)
{
debug = b.equals("true");
}
public void addPropertyChangeListener (PropertyChangeListener l)
{
pcs.addPropertyChangeListener( l );
}
public void removePropertyChangeListener(PropertyChangeListener l)
{
pcs.removePropertyChangeListener( l );
}
public void processRequest (HttpServletRequest req)
{
user = req.getParameter(userId);
passwd = req.getParameter(password);
if (debug)
{
if(user == null || user.equals("") )
{
writer.writeDebug("Empty user id parameter");
}
if (passwd == null || passwd.equals("") )
{
writer.writeDebug("Empty password parameter");
}
}
}
public void logon()
{
try
{
con = DriverManager.getConnection( url, user, passwd );
pcs.firePropertyChange(connection, null, con);
if (debug)
{
writer.writeDebug("Connection succeded! URL: " + url
+ " Driver: " + driver + " User: " + user + " Pwd: " + passwd);
}
}
catch(Exception ex)
{
pcs.firePropertyChange( conError, null, ex);
if (debug)
{
writer.writeDebug("Connection failed! URL: " + url
+ " Driver: " + driver + " User: " + user + " Pwd: "
+ passwd + ex.getMessage());
}
}
}
}

测试页面:try1.jsp
调用地址:http://localhost:8080/myjsp/myEshop/try1.jsp?userid=esdata&amp;passwd=eshop123456
jsp内容如下:
<%@ page contentType="text/html;
charset=gb2312" language="java" import="java.sql.*" errorPage="error.jsp" %>
<jsp:useBean id="mySqlCon" scope="page" class="myeshopbean.ConnectionManager"/>
<%
mySqlCon.setDebug("true");
mySqlCon.processRequest(request);
mySqlCon.setDriverClass("com.mysql.jdbc.Driver");
mySqlCon.setConnectionUrl(" jdbc:mysql://localhost/eshop?user=esdata&amp;password=eshop123456");
mySqlCon.logon();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
<title>无标题文档</title>
</head>
<p>测试页面!</p>
<body>
</body>
</html>
 
不好意思!!!打扰各位了,我刚把代码贴上来,然后在这个页面里看了,发现在我的jsp页面中的
mySqlCon.setConnectionUrl(" jdbc:mysql://localhost/eshop?user=esdata&amp;password=eshop123456");
在字符串前多了一个空格,我立即在我本地上将该空格删除问题就解决了....
真是"郁闷",这种问题让我想了两天.................
不过,也觉得那个提示太简单了吧,让我找得好辛苦.............
谢谢大家了
 
多人接受答案了。
 
后退
顶部