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)
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.
Comments