0

Whats wrong with my JSP code here... The java file runs successfully but when I invoke the method through JSP, it doesn't work on throws an error where obj.insertData() method is called.

<%@ page import="lvsdummy.AddCustomer, java.sql.*"%>
<html>
<head>
<title>Welcome page</title>
</head>
<body>
    <%
        AddCustomer obj = new AddCustomer();
        obj.openConn();
        obj.insertData();
    %>

</body>
</html>

Here's the Java file-

package lvsdummy;

import java.sql.*;

public class AddCustomer {
    String url = "jdbc:mysql://localhost:3306/lvs_db";
    String user_name = "root";
    String password = "root";

    Connection conn;
    PreparedStatement st;
    ResultSet rs;

    public void openConn() {
        try {
            conn = DriverManager.getConnection(url, user_name, password);

        } catch (SQLException e) {
            e.getLocalizedMessage();
        }
    }

    public String insertData() {
            String value = "";
            try {
                st = conn.prepareStatement("SELECT * FROM customer");
                //st.setString(1, firstname);
                //st.setLong(2, mobile1);
                rs = st.executeQuery();

                if (rs.next()) {
                    value = "Success";
                } else {
                    value = "Failed";
                }

            } catch (SQLException e) {
                e.getLocalizedMessage();
            }

            return value;
        }

    public static void main(String[] args) {
        AddCustomer obj = new AddCustomer();
        obj.openConn();
        System.out.println(obj.insertData());
    }
}

Here's the error -

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

7:  <%
8:      AddCustomer obj = new AddCustomer();
9:      obj.openConn();
10:         obj.insertData();
11:     %>
12: 
13: </body>

root cause
java.lang.NullPointerException
    lvsdummy.AddCustomer.insertData(AddCustomer.java:26)
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Pradeep Simha Dec 16 '16 at 13:32
  • 2
    You should actually print `e.getLocalizedMessage()` so you can see what's wrong. – Zircon Dec 16 '16 at 13:36
  • Take a look at implementation of method AddCustomer.insertData. Probably you have some fields that are not initialized. – Sergii Bishyr Dec 16 '16 at 13:37
  • Did you see you are calling a method called insrtData and executes a select?? – cralfaro Dec 16 '16 at 13:37

3 Answers3

0

That's some seriously nasty code you're writing there

  1. You are using scriptlets in a JSP, always a bad idea. You should consider using an MVC framework
  2. You are never closing the database connection. Connections should always be closed in a finally clause
  3. Having url/username/password as properties on the AddCustomer is just plain wrong. You should consider using a dependency injection framework
lance-java
  • 21,832
  • 3
  • 45
  • 81
  • Hey Lance, thanks for those suggestion, I will use all of them as I go through my learning... i'm new to this and starting with basics... stuck here from too long... – Sagar Sutar Dec 16 '16 at 13:46
0

In addition to Lance Java:

Did you check whether openConn() created a connection successfully? As you catch the exception and doesn't do much with it. You should log/print it and throw another exception as you can't proceed without a connection.

CornePlas
  • 31
  • 2
  • Hey... As I said I did check the same in Java itself... and it works fine... I am unsure if am calling the method in wrong way... – Sagar Sutar Dec 16 '16 at 13:45
0

Please try to avoid scriptlets as said above. One more point is that did you anywhere loaded the driver? Please try using if not yet

Class.forName("com.mysql.jdbc.Driver");

Hope that helps