我的PreparedStatement中的NullPointerException?

时间:2015-11-17 10:02:02

标签: java jdbc nullpointerexception prepared-statement

所以这是我第一次用Java编写数据库连接到DB2数据库,到目前为止我已经收到了帮助,但现在我遇到了一个N​​ullPointerException,当我尝试通过按下按钮执行SQL查询时收到。 NullPointerException似乎发生在我用于SQL查询的PreparedStatement中。我正在使用MVC模型和DAO模式。

我的控制器类(仅显示重要部分):

public class Controller implements Observer, ActionListener{

Connection dbConnection = null;
public static PreparedStatement preparedStatement = null;

@Override
public void actionPerformed(ActionEvent e) {
    switch (e.getActionCommand()) {
    case View.SEARCH:
        try {

            String name = View.searchname.getText();
            String plz = View.searchplz.getText();
            String result = "SELECT BBSTBBNR, BBSTNABEG, BBSTPLZ FROM BP.TBBBST WHERE BBSTNABEG = name AND BBSTPLZ = plz";

            dbConnection = ConnectionManager.getConnection();
            PreparedStatement preparedStatement = dbConnection.prepareStatement(result);


            model.search();
        } catch (SQLException e1) {
            System.out.println("An SQL-Error occured: ");
            e1.printStackTrace();
        } catch (IOException e1) {
            System.out.println("An error occured: ");
            e1.printStackTrace();
        }
        break;

    default:
        System.out.println("Search error: " + e.getActionCommand());
        break;
    }

}

我的BtrDao课程:

public interface BtrDao {

Collection<BTRBean> getBTR() throws SQLException, IOException;
}

我的DaoImpl课程:

public class BtrDaoImpl extends AbstractDao implements BtrDao {

public Collection<BTRBean> getBTR() throws SQLException,
        IOException {
    final Collection<BTRBean> result = new ArrayList<BTRBean>();

    ResultSet resultset = null;
    try {
        //This is where the Nullpointerexception happens!
        resultset = Controller.preparedStatement.executeQuery();

        // while loop for finding all results
        while (resultset.next()) {
            BTRBean btr = new BTRBean();
            int btrid = resultset.getInt(1);
            String btrplz = resultset.getString(2);
            String btrname = resultset.getString(3);
            result.add(btr);
            System.out.println(btrid + " " + btrplz + " " + btrname);
        }

    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("There was an SQL processing error: (getBTR)");
        e.printStackTrace();
    } catch (NullPointerException npe) {
        System.out.println("NullPointerException");
        npe.printStackTrace();
    } finally {

        closeConnection(resultset);

    }

    return result;
}

}

我的AbstractDao课程:

public class AbstractDao {

public static Connection getConnection() throws SQLException {
    return ConnectionManager.getInstance().getConnection();
}

public ResultSet getResultset(final String statement) throws SQLException {
    final Statement stmnt = getConnection().createStatement();
    return stmnt.executeQuery(statement);
}

public void closeConnection(final ResultSet resultset) throws SQLException {
    if(resultset != null)
    resultset.close();
    getConnection().close();
}
}

我的ConnectionManager类:

public class ConnectionManager {

    public Connection getConnection() throws SQLException {

        try {
        Properties props = new Properties();
        FileInputStream in = new FileInputStream(".//required.\\dbprop.\\DBConn.properties");
        props.load(in);
        in.close();
        String drivers = props.getProperty("jdbc.drivers");
        if (drivers !=null) 
            System.setProperty("jdbc.drivers", drivers);

        String url = props.getProperty("jdbc.url");
        if (url !=null)
            System.setProperty("jdbc.url", url);

        String username = props.getProperty("jdbc.username");
        if (username !=null)
            System.setProperty("jdbc.username", username);
        String password = props.getProperty("jdbc.password");
        if (password !=null)
            System.setProperty("jdbc.password", password);

        return DriverManager.getConnection(url, username, password);
        } catch (IOException e) {
            throw new SQLException(e.getMessage(), e.getCause());
        }

    }


    //Close connection
    public void closeConnection(Connection con) throws SQLException {
        if (con != null)
            con.close();
    }

    // Making sure that there's only one instance of the class
    private static ConnectionManager singleton_Instance;

    // default constructor
    private ConnectionManager() {
    }

    // returns the single instance of the class
    public static ConnectionManager getInstance() {
        if (singleton_Instance == null) {
            singleton_Instance = new ConnectionManager();
        }
        return singleton_Instance;
    }

}

对于我的ConnectionManager,从属性文件中读取数据库驱动程序,URL,用户名和密码。 (是的,这就是我应该这样做的方式)

我一直坐在这个问题上一整天,每当我尝试修复它时,会弹出另一个错误。任何人都可以告诉我哪个部分是错的以及我如何改进/修复它?

0 个答案:

没有答案