java web应用程序mysql连接错误

时间:2014-02-06 06:28:39

标签: java mysql jsp

我是Java新手。我的Java环境是1. Windows 8. 2. Mysql - 5.5.27。 3. Eclipse IDE for Java EE Devoper v-2。 5. Apache Tomcat v-7。 6. mysql-connector-java-5.1.28 bin rar 7. JDK 1.7。现在我想从jsp文件连接mysql。我已经将mysql-connector添加到库资源中。但我无法连接。我在下面给出了我的代码和错误。 代码:home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page language="java" import="java.sql.Connection"%>
<%@ page language="java" import="java.sql.PreparedStatement"%>
<%@ page language="java" import="java.sql.ResultSet"%>
<%@ page language="java" import="java.sql.SQLException"%>
<%@ page language="java" import="java.sql.DriverManager"%>
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bengal Contact List | Home </title>
</head>
<body>
<%

Connection con = null;
try {
//Class.forName("com.mysql.jdbc.Driver");
String driver="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/java_contact";
String user = "root";
String password = "";
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
}
catch(ClassNotFoundException cnfe){
System.out.println(cnfe);
}
catch(SQLException ex){
System.out.println(ex);
}

// Connection con;
// con=DBConnect.GetDBConnect();
String sql="SELECT * FROM contactsinfo";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery(sql);

%>
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Designation</td>
</tr>
<%
  while(rs.next()){
%>
<td><%=rs.getInt("ID")%></td>
<td><%=rs.getString("Name")%></td>
<td><%=rs.getString("Designation")%></td>
<%
}
%>
</table>
 <h1>Now i am in Home Page.I want to show table information of Contact List.</h1>
</body>
</html>

错误:

type Exception report

message 

description The server encountered an internal error () that prevented it from   fulfilling this request.

exception 

org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.NullPointerException
org.apache.jsp.home_jsp._jspService(home_jsp.java:96)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.

请找出问题所在。

4 个答案:

答案 0 :(得分:0)

尝试对rs.getInt("ID"), rs.getString("Name"), rs.getString("Designation")进行空检查。 NullPointerException正在页面中投放。

也用于con对象。

答案 1 :(得分:0)

首先检查你的结果是否为空

ResultSet rs = statement.execute();
if (!rs.next()){
   //ResultSet is empty
}
else{
   <td><%=rs.getInt("ID")%></td>
   <td><%=rs.getString("Name")%></td>
   <td><%=rs.getString("Designation")%></td>
}

答案 2 :(得分:0)

你必须添加数据库(3306)端口号,用户名和密码的端口号,并附上工作代码..检查并更新评论......

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ page language="java" import="java.sql.Connection"%>
    <%@ page language="java" import="java.sql.PreparedStatement"%>
    <%@ page language="java" import="java.sql.ResultSet"%>
    <%@ page language="java" import="java.sql.SQLException"%>
    <%@ page language="java" import="java.sql.DriverManager"%>
    <%@ page language="java" import="java.util.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Bengal Contact List | Home </title>
    </head>
    <body>
    <%

    Connection con = null;
    try {
    //Class.forName("com.mysql.jdbc.Driver");
    String driver="com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/schemaname";
    String user = "username";
    String password = "password";
    Class.forName(driver);
    con = DriverManager.getConnection(url, user, password);
    }
    catch(ClassNotFoundException cnfe){
    System.out.println(cnfe);
    }
    catch(SQLException ex){
    System.out.println(ex);
    }

    // Connection con;
    // con=DBConnect.GetDBConnect();
    String sql="SELECT * FROM contactsinfo";
    PreparedStatement ps = con.prepareStatement(sql);
    ResultSet rs = ps.executeQuery(sql);

    %>
    <table border="1">
    <tr>
    <td>ID</td>
    <td>Name</td>
    <td>Designation</td>
    </tr>
    <%
      while(rs.next()){
    %>
    <td><%=rs.getInt("ID")%></td>
    <td><%=rs.getString("Name")%></td>
    <%
    }
    %>
    </table>
     <h1>Now i am in Home Page.I want to show table information of Contact List.</h1>
    </body>
    </html>

答案 3 :(得分:0)

首先避免使用scriptlet“&lt; %%&gt;”在JSP中。

其次总是在代码中执行null检查,尤其是在使用db result时。你正在JSP中出现空指针异常,它出现在这里;

 <td><%=rs.getInt("ID")%></td>
 <td><%=rs.getString("Name")%></td>
 <td><%=rs.getString("Designation")%></td>

请尝试那样的; - 在文件上输入此顶部;

  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

然后

 <c:if test="MAKE_YOUR_NULL_CHECK_HERE">
       //Write your <td> here.
 </c:if>