NullPointerException问题,在线等
我有个query.jsp页面还有个OpenDB.java类,OpenDB.java代码如下:
package com.netshop;
import java.sql.*;
public class OpenDB
{ String userName="root";
String userPassword="111111";
private String driverName="org.gjt.mm.mysql.driver";
private String url="jdbc:mysql://localhost/netshop?user="+userName+"&password="+userPassword;
Connection dbConn;
public OpenDB()
{
}
public Connection getConnection()
{
try
{
Class.forName(driverName);
dbConn=DriverManager.getConnection(url);
}
catch(Exception ex)
{
System.out.println(ex.toString());
dbConn=null;
}
return dbConn;
}
}
query.jsp代码如下:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*"
errorPage="" %>
<%@page import="com.netshop.*"%>
<jsp:useBean scope="page" id="at" class="com.netshop.OpenDB"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<%Connection conn=at.getConnection();
Statement stmt=conn.createStatement();%>
<%String sql="select * from attention";%>
<%ResultSet rs=stmt.executeQuery(sql);%>
<%if(rs.next()){%>
java bean name is :
<%=rs.getLong("tag")%>
<%}%>
</body>
</html>
出错在Statement stmt=conn.createStatement();%>这行,ROOT cause提示是
java.lang.NullPointerException,顺便说下我mysql数据库表里有记录,请问这是怎么回事?
------解决方案--------------------Statement stmt=conn.createStatement();既然是这里出错,那很明显就是conn为null嘛!
driverName="org.gjt.mm.mysql.driver; 我没看到过你这样的写法,改成:com.mysql.jdbc.Driver这个试下,说不定就可以了!
------解决方案--------------------
用这么低的版本!我用的是:mysql-connector-java-5.0.8-bin.jar
dbConn=DriverManager.getConnection(url,userName,userPassword);
private String url="jdbc:mysql://localhost:3306/netshop
我都是这样用的,没任何问题!建议你换个高版本的看看!
------解决方案--------------------不过,最好还是不要在jsp页面写java代码,这种写法不大好,而且看起来很乱,调试起来也不大方便。