Whats wrong with this JSP code

Sagar Sutar

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)
Subhasish Bhattacharjee

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javascript Whats wrong with this code

From Dev

Whats wrong in the following code?

From Dev

whats wrong with the android code ?

From Dev

Whats wrong with this java code

From Dev

Whats wrong with this glsl code?

From Dev

Whats wrong with this piece of code?

From Dev

Whats wrong with this C code lines

From Dev

Whats wrong with this code to read file?

From Dev

Whats wrong with this code? replace method

From Dev

Whats is wrong with this real mode code

From Dev

Whats wrong with this code to read file?

From Dev

Whats wrong with my jQuery code?

From Dev

Whats wrong with my php code?

From Dev

Whats wrong with this code? Objective C

From Dev

Whats wrong with this Jquery selector code?

From Dev

Whats wrong with the given Java Code

From Dev

whats wrong with this code of reading with mmap?

From Dev

Whats wrong with my sql code

From Dev

Process finished with exit code 0 - whats wrong?

From Dev

whats wrong with my capitalize letters in words code?

From Dev

new to c, whats wrong with my code

From Dev

WHats wrong with this code for palindrome of a string array

From Dev

Whats wrong with this c code(linked list implementation)?

From Dev

Whats wrong with my jQuery Code for RadioButtonList

From Dev

Can someone look at my code and tell me whats wrong?

From Dev

Whats wrong in this code? im trying to make a login page

From Dev

Whats wrong with my code?(changing pictures using javascript)

From Dev

Can any body tell whats wrong with this code (New to python:()

From Dev

Whats wrong with my code? Android simple list view example