首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

DB2连接报java.lang.NullPointerException异常

2014-01-26 
DB2连接报java.lang.NullPointerException异常?我的代码如下: package com import java.sql.* import jav

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();         

热点排行