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.