1

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.

Mahi Uddin
  • 11
  • 2
  • Please avoid using of scriptlets `<% %>` in JSP. See [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1031945) – Aniket Kulkarni Feb 06 '14 at 06:32
  • try to give null checks for rs.getInt("ID"), rs.getString("Name"), rs.getString("Designation"). NullPointerException is throwing there in the page. – TKV Feb 06 '14 at 06:34

4 Answers4

0

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.

TKV
  • 2,533
  • 11
  • 43
  • 56
  • <% Integer id; id=rs.getInt("ID"); if(id != null){ System.out.println(rs.getInt("ID")); } %> <% String name; name=rs.getString("Name"); if(name != null){ System.out.println(rs.getString("Name")); } %> <% String designation; designation=rs.getString("Designation"); if(designation != null){ System.out.println(rs.getString("Designation")); } %> <% } %> I add this. But same error show. – Mahi Uddin Feb 06 '14 at 07:01
0

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>
}
Sathesh
  • 39
  • 1
  • 5
  • Thanks Sathesh. I tried. But error show - org.apache.jasper.JasperException: An exception occurred processing JSP page /home.jsp at line 37 36: String sql="SELECT * FROM contactsinfo"; 37: PreparedStatement ps = con.prepareStatement(sql); 38: //ResultSet rs = ps.executeQuery(sql); 39: ResultSet rs = ps.executeQuery(); 40: if (!rs.next()){ – Mahi Uddin Feb 06 '14 at 07:55
0

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>
Krishna S
  • 17
  • 7
  • org.apache.jasper.JasperException: An exception occurred processing JSP page /home.jsp at line 37 34: // Connection con; 35: // con=DBConnect.GetDBConnect(); 36: String sql="SELECT * FROM contactsinfo"; 37: PreparedStatement ps = con.prepareStatement(sql); 38: //ResultSet rs = ps.executeQuery(sql); 39: ResultSet rs = ps.executeQuery(); 40: if (!rs.next()){ error show. it will be very helpful for me. If you send me a fresh copy of jsp file. where you connect with database and show the data. – Mahi Uddin Feb 06 '14 at 07:59
  • i want your above error running code.can you post?then i want following information database username and password, schemaname,table name – Krishna S Feb 06 '14 at 08:58
  • Thanks. i got the answer. actually i added the mysql-connector jar file to java library folder. now i add the jar file to the web-content web-inf/lib. now its ok. – Mahi Uddin Feb 07 '14 at 05:14
0

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>
Semih Eker
  • 2,389
  • 1
  • 20
  • 29
  • Thanks. i got the answer. actually i added the mysql-connector jar file to java library folder. now i add the jar file to the web-content web-inf/lib. now its ok. – Mahi Uddin Feb 07 '14 at 05:15
  • congratulations to you :) But you can perform that I wrote in your code, these are important in Spring MVC and JSP. – Semih Eker Feb 07 '14 at 06:21