Forums Register Login

Using MySql with a Java Web App

+Pie Number of slices to send: Send
I'm currently building a database app where the web app sits on Applications Server 9.1 and after a user fills in a form it the information is sent to a MySql database.

When I press submit I get a java.lang.NullPointerException. I have placed the driver in the jre/lib/ext folder and the code for connecting to the database is as follows:

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pd206",
"root", "password");
} catch (ClassNotFoundException e) {
System.out.println("Class still not found");
} catch (SQLException e) {
System.out.println("SQL Exception in init");
}

The system is currently working on a local machine and is not expected to go onto a full network until later in the year.

Can somebody please help?
+Pie Number of slices to send: Send
In which line does the exception occur? Looking at this code, I wouldn't think that any of it can throw a NPE.
+Pie Number of slices to send: Send
it doesnt come up with a line on the code. Its when I press the submit button on the web page of the app it goes to the error message. The first line on the stack trace is:

Servlet.service() for servlet initialEnq threw exception java.lang.NullPointerException at initalEnq.submitData(initialEnq.java:63)
+Pie Number of slices to send: Send
So what's happening on line 63?
+Pie Number of slices to send: Send
line 63 is the last line of this code section which shows everything upto that point. it is basically where it tries to create the statement. I keep thinking it is something to do with the mySql driver because I am trying to run it on the machine it will work from normally, however it works fine on my developer machince when launched from NetBeans

public class initialEnq extends HttpServlet {

Connection con;

public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pd206",
"root", "password");
} catch (ClassNotFoundException e) {
System.out.println("Class still not found");
} catch (SQLException e) {
System.out.println("SQL Exception in init");
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String a = request.getParameter("number");
String b = request.getParameter("rank");
String c = request.getParameter("surname");
String d = request.getParameter("forenames");
String e = request.getParameter("trade");
String f = request.getParameter("unit");
String g = request.getParameter("unitAddress");
String h = request.getParameter("workContactNo");
String i = request.getParameter("mobileContactNo");
String j = request.getParameter("actionTaken");
String k = request.getParameter("actionRequired");
String l = request.getParameter("submitedBy");
String m = request.getParameter("dateCompleted");

submitData(a, b, c, d, e, f, g, h, i, j, k, l, m);

RequestDispatcher view = request.getRequestDispatcher("submited.jsp");
view.forward(request, response);
}

public void submitData(String a, String b, String c, String d, String e, String f,
String g, String h, String i, String j, String k, String l, String m) {

try {
Statement stmt = con.createStatement();
+Pie Number of slices to send: Send
 

Originally posted by dave green:
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pd206",
"root", "password");
} catch (ClassNotFoundException e) {
System.out.println("Class still not found");
} catch (SQLException e) {
System.out.println("SQL Exception in init");
}
}


This method is a red flag to me. If an error occurs loading the driver or getting the connection, the method quietly logs it and then goes on. If the connection cannot be retrieved, I would throw a runtime exception so the servlet doesn't become available to users. This will tell you more specifically what the error is without failing with a null pointer.

Also, I recommend including the exception in your error message as it will give you a bigger clue as to what is going wrong. Feel free to post the error message here for more help.
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1025 times.
Similar Threads
Cannot open connection issue
MySQL
classnotfound Java->mysql jdbc problem!
MySQL connection Problem
connect my MySQl databse
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:17:04.