未找到列访问数据库

时间:2017-10-06 20:22:13

标签: java swing ms-access

我正在创建一个地址簿应用程序,我正在研究搜索功能。每当我搜索时,我都会找到一个未找到的列#34;例外。这是我的代码:

else if(actionCommand.contentEquals("search"))
        {
        String searchInput = searchBar.getText();
        //String[] searchSplit = searchInput.split("\\s");
        //String searchFirst = searchSplit[0];
        //String searchLast = searchSplit[1];
        personID = null;
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection connection = DriverManager.getConnection("jdbc:odbc:address_book");
            Statement statement = connection.createStatement();

            ResultSet resultSet = statement.executeQuery("SELECT (personID) FROM names WHERE (lastName) LIKE ('"+searchInput+"')");
            if(resultSet.next())
                personID = resultSet.getString("personID");

            ResultSet firstQuery = statement.executeQuery("SELECT (firstName) FROM names WHERE (personID) LIKE ('"+personID+"')");
            if(firstQuery.next())
                firstNameOutput = firstQuery.getString("firstName");

            ResultSet lastQuery = statement.executeQuery("SELECT (lastName) FROM names WHERE (personID) LIKE ('"+personID+"')");
            if(lastQuery.next())
                lastNameOutput = lastQuery.getString("lastName");

            ResultSet address1Query = statement.executeQuery("SELECT (address1) FROM addresses WHERE (personID) LIKE ('"+personID+"')");
            if(address1Query.next())
                address1Output = address1Query.getString("address1");

            ResultSet address2Query = statement.executeQuery("SELECT (address2) FROM addresses WHERE (personID) LIKE ('"+personID+"')");
            if(address2Query.next())
                address2Output = address2Query.getString("address1");

            ResultSet cityQuery = statement.executeQuery("SELECT (city) FROM addresses WHERE (personID) LIKE ('"+personID+"')");
            if(cityQuery.next())
                cityOutput = cityQuery.getString("city");

            ResultSet stateQuery = statement.executeQuery("SELECT (state) FROM addresses WHERE (personID) LIKE ('"+personID+"')");
            if(stateQuery.next())
                stateOutput = stateQuery.getString("state");

            ResultSet zipcodeQuery = statement.executeQuery("SELECT (zipcode) FROM addresses WHERE (personID) LIKE ('"+personID+"')");
            if(zipcodeQuery.next())
                zipcodeOutput = zipcodeQuery.getString("zipcode");

            ResultSet phoneQuery = statement.executeQuery("SELECT (phoneNumber) FROM phoneNumbers WHERE (personID) LIKE ('"+personID+"')");
            if(phoneQuery.next())
                phoneOutput = phoneQuery.getString("phoneNumber");

            ResultSet emailQuery = statement.executeQuery("SELECT (emailAddress) FROM emailAddresses WHERE (personID) LIKE ('"+personID+"')");
            if(emailQuery.next())
                emailOutput = emailQuery.getString("emailAddress");

            newFunctionWindow();
            personIDBar.setText(personID);
            firstNameBar.setText(firstNameOutput);
            lastNameBar.setText(lastNameOutput);
            address1Bar.setText(address1Output);
            address2Bar.setText(address2Output);
            cityBar.setText(cityOutput);
            stateBar.setText(stateOutput);
            zipcodeBar.setText(zipcodeOutput);
            phoneBar.setText(phoneOutput);
            emailBar.setText(emailOutput);
            statement.close();
            connection.close();
        }

        catch (SQLException sqlException)
        {
            JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Database Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        catch (ClassNotFoundException classNotFound)
        {
            JOptionPane.showMessageDialog(null, classNotFound.getMessage(), "Driver Not Found", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

我对所有列名称进行了三重检查,并且没有拼写错误。我相信我的所有SQL语句都是正确的,所以我不太清楚问题是什么!谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

        ResultSet address1Query = statement.executeQuery("SELECT (address1) FROM addresses WHERE (personID) LIKE ('"+personID+"')");
        if(address1Query.next())
            address1Output = address1Query.getString("address1");

        ResultSet address2Query = statement.executeQuery("SELECT (address2) FROM addresses WHERE (personID) LIKE ('"+personID+"')");
        if(address2Query.next())
            address2Output = address2Query.getString("address1");

哎呀 - 来自address2字段的地址1?那不行。 (最后一行引用)