This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC and the fly likes Using MySql with a Java Web App Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Using MySql with a Java Web App" Watch "Using MySql with a Java Web App" New topic
Author

Using MySql with a Java Web App

dave green
Greenhorn

Joined: Oct 15, 2007
Posts: 10
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?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
In which line does the exception occur? Looking at this code, I wouldn't think that any of it can throw a NPE.


Ping & DNS - my free Android networking tools app
dave green
Greenhorn

Joined: Oct 15, 2007
Posts: 10
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)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
So what's happening on line 63?
dave green
Greenhorn

Joined: Oct 15, 2007
Posts: 10
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();
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30392
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using MySql with a Java Web App