简体   繁体   English

Java Web应用程序mysql连接错误

[英]java web application mysql connection error

I am new in Java. 我是Java新手。 My Java Environment is 1. Windows 8. 2. Mysql - 5.5.27. 我的Java环境是1. Windows8。2. Mysql-5.5.27。 3. Eclipse IDE for Java EE Devoper v-2. 3. Java EE Devoper v-2的Eclipse IDE。 5. Apache Tomcat v-7. 5. Apache Tomcat v-7。 6. mysql-connector-java-5.1.28 bin rar 7. JDK 1.7. 6. mysql-connector-java-5.1.28 bin rar 7. JDK 1.7。 Now i want to connect with mysql from jsp file. 现在我想从jsp文件与mysql连接。 I already add the mysql-connector to the library resource. 我已经将mysql-connector添加到库资源中。 But i can not connect. 但是我无法连接。 My code and error i given in the following. 我的代码和错误我在下面给出。 Code: home.jsp 代码: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>

Error: 错误:

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.

please find out where is the problem. 请找出问题所在。

try to give null checks for rs.getInt("ID"), rs.getString("Name"), rs.getString("Designation") . 尝试对rs.getInt("ID"), rs.getString("Name"), rs.getString("Designation")进行空检查。 NullPointerException is throwing there in the page. NullPointerException在页面中抛出。

Use the same for con object too. 对于con对象也使用相同的对象。

first check your result is empty or not 首先检查您的结果是否为空

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>
}

you must add port number of the Database(3306) port number, username and password and I have attached working code.. check and update the comment..... 您必须添加数据库(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>

Firstly avoid using of scriptlets "<% %>" in JSP. 首先,避免在JSP中使用scriptlet“ <%%>”。

Secondly always do null check in your code, especially if you are using db result. 其次,总是在代码中执行null检查,尤其是在使用db result的情况下。 You are heving null pointer exception in your JSP and it appear here; 您正在JSP中使用null指针异常,它出现在这里;

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

Please try sthg like that; 请这样尝试。 -put this top on the file; -将此顶部放在文件上;

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

and then 接着

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM