简体   繁体   中英

java web application mysql connection error

I am new in Java. My Java Environment is 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. Now i want to connect with mysql from jsp file. I already add the mysql-connector to the library resource. But i can not connect. My code and error i given in the following. Code: 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") . NullPointerException is throwing there in the page.

Use the same for con object too.

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

<%@ 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.

Secondly always do null check in your code, especially if you are using db result. You are heving null pointer exception in your JSP and it appear here;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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