Communicating With SQL Server via HTML

[nobr]Hello.

I have recenty reintroduced myself to the Java world after having taken a class in college a few years back. I am trying to retrieve information from a SQL Server based on user input on an HTML form and display it back to HTML. From examples found online and an example from the class that connected to an access DB, I have put the following code together. I know it's not complete but I am stuck. I get a null pointer error on the line: stmt = con.createStatement(); I can't figure it out. I have verified that I can connect to the SQL Server through a command line program but I can't seem to apply it to this scenario. Can someone please help me?!

package coreservlets;

import java.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

import java.util.*;

publicclass exiTrackingextends HttpServlet{

publicstaticfinal String url ="jdbc:microsoft:sqlserver://";

publicstaticfinal String serverName="server4";

publicstaticfinal String portNumber ="1433";

publicstaticfinal String databaseName="DB";

publicstaticfinal String userName ="user";

publicstaticfinal String password ="pass";

publicstaticfinal String selectMethod ="cursor";

publicstatic java.sql.Connection con;

publicstatic Statement stmt;

publicstatic String query ="select custid from tarcustomer";

/****************************************************************************

DOPOST - GATHERS AND POSTS

*****************************************************************************/

publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String so = request.getParameter("sono");//get info from html form

String po = request.getParameter("custpono");

String title ="Retrieval Result";

String validationResult ="";

String redirect ="";

String headTag ="<HTML><HEAD><TITLE>"+title+"</TITLE>";

String selectResult ="";

String endHeadTag ="</HEAD>";

String resultString ="invalid";

try{

con = this.getConnection();

stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);

while (rs.next()){

String s = rs.getString("custid");

if(s.equalsIgnoreCase(so)){

resultString ="valid";//password is found

break;}

}

rs.close();

stmt.close();

}

catch (SQLException e){

System.err.println(e.getMessage());

}

if(selectResult =="valid")

{

redirect ="<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1;URL=http://java.sun.com\">";

validationResult ="Successful query. Well done chap. <br>You will be re-directed to our homepage momentarily.";

}

else{

validationResult ="Unsuccessful Search. " +so+" " +po+"<br><Br>" +selectResult;

redirect ="<META HTTP-EQUIV=\"Refresh\" CONTENT=\"2;URL=/sotracking/html/index.html\">";

}

String outString = headTag + redirect + endHeadTag +

"<BODY BGCOLOR=\"#FDF5E6\">\n" +

"<H1 ALIGN=\"CENTER\">"+validationResult+"</H1>\n"+

"</BODY></HTML>";

out.println(outString);

try{

con.close();

}

catch (SQLException e){

System.err.println(e.getMessage());

}

}

/*********************************************************

GET CONNECTION

**********************************************************/

private java.sql.Connection getConnection(){

try{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);

//if(con!=null);

}catch(Exception e){

e.printStackTrace();

System.out.println("Error Trace in getConnection() : " + e.getMessage());

}

return con;

}

/************************************************************

CONNECTION URL

***************************************************************/

privatestatic String getConnectionUrl(){

return url+serverName+";databaseName="+databaseName+";selectMethod="+selectMethod+";";

}

}

[/nobr]

[8497 byte] By [sabian131a] at [2007-11-27 2:39:02]
# 1

con must be null to get an NPE in that line, therefore you're failing to acquire a connection.

Since you have some logging, you ought to be seeing this message as well:

"Error Trace in getConnection()"

Usually that means that you've not put the JDBC driver JAR into the class path of the application.

dcmintera at 2007-7-12 3:00:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Thanks for the quick reply!

As I mentioned originally, it's been a while since so please bear with me. I really appreciate your help.

While I am doing some logging, it is purely by accident as I am copying some text from previous code. I don't remember where or how to view the logs.

As far as the environment I am running, I am using an app called JEDPlus to edit and compile code for this program. After downloading and installing the Microsoft SQL 2000 Driver for JDBC (including SP3) I used a comand line compiler for a command line program that succesfully connected to a SQL Server to retrieve information based on a command line argument and am using the classpath setup (as well as the other code for connecting to SQL Server) from that and applying it here. I believe I am pointing to the mssqlserver.jar / msutil.jar / msbase.jar / as well as the serlvet.jar files.

sabian131a at 2007-7-12 3:00:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

In the JEDPlus application, I am pointing the javac compiler path. I know that is correct. There is a section for additional command line arguments and this is what I have:

-classpath .;c:\Progra~1\Allaire\JRun\lib\ext\servlet.jar;C:\PROGRA~1\MI512C~1\lib\msbase.jar;C:\PROGRA~1\MI512C~1\lib\msutil.jar;C:\PROGRA~1\MI512C~1\lib\mssqlserver.jar

sabian131a at 2007-7-12 3:00:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
The classpath for the compiler and the classpath for the application when you run it don't particularly have to be the same.I don't know anything about the IDE you're using, so I can't really help there.
dcmintera at 2007-7-12 3:00:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Ah hah!! The classpath for the application....I am running JRun. I found where I needed to add the classpath information as well as where I could view the logs. I believe I am well on my way (for now). Thanks so very much!! You have been a huge help!!
sabian131a at 2007-7-12 3:00:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...