Any idea?
I get this error:

org.apache.jasper.JasperException: An exception occurred processing JSP page /test2.jsp at line 7

4: <title>DB Test</title>
5: </head><body>
6: <% DBConnect d = new DBConnect();
7: ResultSet rs = d.getResult("SELECT name FROM items");
8:
9: while (rs.next()) {
10: String name = rs.getString("name");


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.lang.NullPointerException
com.monkeygarage.DBConnect.getResult(Unknown Source)
org.apache.jsp.test2_jsp._jspService(test2_jsp.java:61)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


Here's the code for the JSP:

<%@page import="com.monkeygarage.DBConnect,java.sql.*"%>
<html>
<head>
<title>DB Test</title>
</head><body>
<% DBConnect d = new DBConnect(); 
     ResultSet rs = d.getResult("SELECT name FROM items"); 

          while (rs.next()) { 
               String name = rs.getString("name"); 
              System.out.println(name);}
                   %>
</body>
</html>

And I should mention, when I run the DBconnect.java bit in Eclipse, it works just fine and actually will return the answer to:

public static void main(String[] args) { 
         try { 
              DBConnect d = new DBConnect(); 
              ResultSet rs = d.getResult("SELECT name FROM items"); 

              while (rs.next()) { 
                   String name = rs.getString("name"); 
                   System.out.println(name); 
              } 
         } catch (SQLException ex) { 
              ex.printStackTrace(); 
         } 
    }

which is simply, mustang

It is bad practice to connect to DB from JSP. JSP is not supposed to do so, it is Servlet what you should use. JSP should have minimum of Java coding inside it...

Where have you kept the mysql-connectorxxx.jar file in your web application. It should be stored inside the $CATALINA_HOME/lib directory and the Tomcat server needs to be restarted once it is placed there so that the JVM of Tomcat can find the mysql specific JDBC drivers at runtime and establish connection with mysql.

It is actually better to place it inside of WEB-INF/lib, unless you are certain that it is needed by, and will not interfere with, other applications on the same server.

The issue was resolved :-) thanks for helping

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.