Resultset没有从PreparedStatement获取一个变量的值?

时间:2015-11-20 12:41:06

标签: java sql db2 prepared-statement resultset

我目前的SQL Query的PreparedStatement存在问题。 我正在尝试创建一个程序,用户可以使用1或2个变量搜索数据库。后一部分,使用2个变量进行搜索没有问题。但是,当尝试仅使用用户输入的一个变量时,我的Resultset似乎没有从PreparedStatement获取值(我在调试器中检查并确认它是一个值)。现在我想知道我做错了什么,特别是因为它似乎之前有两个变量。

我的相关代码:

public class BTRDaoImpl extends AbstractDao implements BTRDao {

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

public static void sqlquery() {
    try {

        String btrname = View.searchbtrname.getText();
        String btrplz = View.searchbtrplz.getText();

        if (btrplz.isEmpty()) {
            String btrResult = "SELECT * FROM BP.TBBBST WHERE BBSTNABEG = TRIM(UPPER(?))";

            dbConnection = AbstractDao.getConnection();
            preparedStatement = dbConnection.prepareStatement(btrResult);
            preparedStatement.setObject(1, btrname);

            Model.search();
        } else {
            String btrResult = "SELECT * FROM BP.TBBBST WHERE BBSTNABEG = TRIM(UPPER(?)) AND BBSTPLZ = TRIM(UPPER(?))";

            dbConnection = AbstractDao.getConnection();
            preparedStatement = dbConnection.prepareStatement(btrResult);
            preparedStatement.setObject(1, btrname);
            preparedStatement.setObject(2, btrplz);

            Model.search();
        }
    } catch (SQLException e1) {
        System.out.println("A SQL error occured: ");
        e1.printStackTrace();
    } catch (IOException e1) {
        System.out.println("An error occured: ");
        e1.printStackTrace();
    }
}

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

    ResultSet resultset = null;
    try {
        resultset = preparedStatement.executeQuery();

        // while loop for returning all found data
        while (resultset.next()) {
            BTRBean btr = new BTRBean();
            int btrid = resultset.getInt(1);
            String btrplz = resultset.getString(2);
            String btrname = resultset.getString(4);
            result.add(btr);
            System.out.println("BTR-ID: " + btrid + " BTR PLZ: " + btrplz + " BTR: " + btrname);
        }

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

        closeConnection(resultset);

    }

    return result;
}

}

我的Model类中的搜索方法:

public static void search() throws SQLException, IOException {
    try {
        BTRDaoImpl btr1 = new BTRDaoImpl();
        btr1.getBTR();
    } catch (SQLException e) {
        System.out.println("SQL Exception (btr1)");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IO Exception (btr1)");
        e.printStackTrace();
    }
}

0 个答案:

没有答案