DB2连接报java.lang.NullPointerException异常?
我的代码如下:
package com;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DB2Bean extends HttpServlet
{
HttpServletResponse response;
private Connection connection;
private Statement stmt;
private ResultSet rs;
public DB2Bean()
throws IOException,ServletException
{
try{
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
String url="jdbc:db2://localhost:5000/HOTEL";
String user="admin";
String password="";
connection= DriverManager.getConnection(url,user,password);
}
catch(ClassNotFoundException cnfex){
System.err.println("connect fail ");
System.err.println(cnfex.getMessage());
}
catch(SQLException sqlex){
System.err.println("can't connect");
}
getTable();
}
public void getTable()
throws IOException,ServletException
{
PrintWriter out =response.getWriter();
try{
String sql="select * from WAREHOUSE";
stmt=connection.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()) {
out.println(rs.getString(1));
out.println(rs.getString(2));
out.println("WWW");
rs.close();
stmt.close();