我写了一个bean,只是用来获得与数据库的连接,
但是,在测试的时候总是有异常。改了多好次,可是仍然还是那个异常,急人!
下面是我写的bean有源代码:
package vcsql;
import java.sql.*;
public class ConnectDB
{
String driverurl = "com.mysql.jdbc.Driver";
String dburl = "jdbc:mysql://localhost:3306/vc";
String dbuser = "root";
String dbpass = "hehe";
Connection conn = null;
public Connection getConnect()
{
try
{
Class.forName(driverurl);
conn = DriverManager.getConnection(dburl,dbuser,dbpass);
}
catch (ClassNotFoundException cnfe)
{
System.out.println("Can not load jdbc driver for mysql");
System.err.println(cnfe);
}
catch (SQLException sqle)
{
System.out.println("SQLException: ");
conn = null;
}
return conn;
}
}
下面是我写的用于测试该bean的JSP程序:
<%@ page import="java.sql.*,vcsql.ConnectDB" %>
<jsp:useBean id="dd" scope="page" class="vcsql.ConnectDB"/>
<%
Connection cc = dd.getConnect();
DatabaseMetaData dma = cc.getMetaData();
out.println("the database is: "+ dma.getURL());
%>
在运行的时候,tomcat提示:在红色那行发生异常
org.apache.jasper.JasperException: An exception occurred processing JSP page /test.jsp at line 8
<%
Connection cc = dd.getConnect();
DatabaseMetaData dma = cc.getMetaData();
out.println("the database is: "+ dma.getURL());
下面还写了:
root cause
java.lang.NullPointerException
我想这应该就是最根本的原因吧。
可是我实在不知道哪里有错。麻烦大家来帮我看一下。