我写了一个连接mysql数据库的servlet,已经成功编程为.class文件,可是就是不能正常运行。
下面是出现的错误信息:
exception
java.lang.NullPointerException
LinkData.doGet(linkdata.java:42)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
我写的程序源代码如下:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.URL;
import java.sql.*;
public class LinkData extends HttpServlet
{
Connection con;
public void init(ServletConfig conf) throws ServletException
{
super.init();
String l = "jdbc:mysql://localhost:3306/jtest";
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(l,"root","hehe");
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>this is a example of servlet link to db</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Servlet link to the database!</h2>");
out.println("<pre>");
try
{
DatabaseMetaData dma = con.getMetaData();
out.println("the database is: "+ dma.getURL());
out.println("Driver is: " + dma.getDriverName());
out.println("Version is: " + dma.getDriverVersion());
out.println("</pre>");
con.close();
}
catch (SQLException sqle)
{
while (sqle != null)
{
out.println("Exception is catched!!!");
out.println(sqle.getMessage());
out.println(sqle.getSQLState());
out.println(sqle.getErrorCode());
sqle = sqle.getNextException();
}
}
out.println("</body>");
out.println("</html>");
}
}
我已经写好了相应的web.xml文件。
我在这个文件中实在找不到哪里用的null类型的对象或者方法了。
请各位帮我看看是什么问题。在下不胜感激!!!
[此贴子已经被作者于2007-8-7 19:41:09编辑过]