0

我正在尝试为我的 gui 编译我的 jdbc,它的目标是从用户那里获取用户输入,然后选择具有相应表的表(尚未实现),然后在该行 imagepath 列下获取字符串该行的图像路径,并使用此字符串显示图像。到目前为止,当我运行我的代码时,我收到以下错误。

java.lang.NullPointerException
    at Assignment.DisplaySearch.Search(Search.java:276)
    at Assignment.Search$4.actionPerformed(Search.java:123)

当我按下以下按钮时会发生此错误。

ViewerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    DisplaySearch.Search(jtfImagename1.getText(), jtfLocation1.getText(), jtfTimestamp1.getText());
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                return;   
            }
        });

JDBC-

 class DisplaySearch {
        // database URL                              

        static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
        static final String USERNAME = "root";
        static final String PASSWORD = "root";

        // launch the application
        public static void Search(String imageName, String DateStamp, String imageTag) throws SQLException {
            Connection connection = null; // manages connection
            Statement statement = null; // query statement
            ResultSet resultSet = null; // manages results

            // connect to database books and query database
            try {
                // establish connection to database
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

                // query database 
                PreparedStatement pt = connection.prepareStatement(
                        "SELECT imageName, DateStamp, imageTag FROM images WHERE imageName=? AND DateStamp=? AND imageTag=?");
                pt.setString(1, "image-name");
                pt.setString(2, "date-stamp"); // maybe .setDate(...)
                pt.setString(3, "image-tag");
                resultSet = pt.executeQuery();

                while (resultSet.next()) {
                    // Position the cursor 
                    imageName = resultSet.getString(0);
                    DateStamp = resultSet.getString(1);
                    imageTag = resultSet.getString(2);
                    System.out.println("datas : " + imageName + " " + DateStamp + " " + imageTag);
                    if (imageName != "" || imageName != null) {
                        String Name = resultSet.getString(imageName);
                        System.out.print("Image Name: " + Name);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageName='"
                                + imageName + "'");
                    }

                    if (DateStamp != null) {
                        String Date = resultSet.getString(DateStamp);
                        System.out.print(", Date: " + Date);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where DateStamp='"
                                + DateStamp + "'");
                    }

                    if (imageTag != "" || imageTag != null) {
                        String Tag = resultSet.getString(imageTag);
                        System.out.print(", Tag: " + Tag);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageTag='"
                                + imageTag + "'");
                    }
                }// end while
            } // end try
            catch (Exception e) {
                e.printStackTrace();
            } // end catch                                                     
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } // end try                                               
            catch (Exception exception) {
                exception.printStackTrace();
            } // end catch 
        } // end main
4

2 回答 2

3

您正在对 PreparedStatement 进行操作,pt但关闭statement为空。

于 2013-10-08T10:39:40.497 回答
0

您正在关闭指向 null 的 Statement 对象,该对象又会给出 NullPointerException。

更改您的代码如下...

class DisplaySearch {
        // database URL                              

        static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
        static final String USERNAME = "root";
        static final String PASSWORD = "root";

        // launch the application
        public static void Search(String imageName, String DateStamp, String imageTag) throws SQLException {
            Connection connection = null; // manages connection
            PreparedStatement  pt = null; // query statement
            ResultSet resultSet = null; // manages results

            // connect to database books and query database
            try {
                // establish connection to database
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

    //            resultSet = statement.executeQuery(
    //                    "SELECT imageName, DateStamp, imageTag FROM images WHERE imageName='*' AND DateStamp='*' AND imageTag='*'");
    //

                // query database 
                pt = connection.prepareStatement(
                        "SELECT imageName, DateStamp, imageTag FROM images WHERE imageName=? AND DateStamp=? AND imageTag=?");
                pt.setString(1, "image-name");
                pt.setString(2, "date-stamp"); // maybe .setDate(...)
                pt.setString(3, "image-tag");
                resultSet = pt.executeQuery();

                while (resultSet.next()) {
                    // Position the cursor 
                    imageName = resultSet.getString(0);
                    DateStamp = resultSet.getString(1);
                    imageTag = resultSet.getString(2);
                    System.out.println("datas : " + imageName + " " + DateStamp + " " + imageTag);
                    if (imageName != "" || imageName != null) {
                        String Name = resultSet.getString(imageName);
                        System.out.print("Image Name: " + Name);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageName='"
                                + imageName + "'");
                    }

                    if (DateStamp != null) {
                        String Date = resultSet.getString(DateStamp);
                        System.out.print(", Date: " + Date);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where DateStamp='"
                                + DateStamp + "'");
                    }

                    if (imageTag != "" || imageTag != null) {
                        String Tag = resultSet.getString(imageTag);
                        System.out.print(", Tag: " + Tag);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageTag='"
                                + imageTag + "'");
                    }
                }// end while
            } // end try
            catch (Exception e) {
                e.printStackTrace();
            } // end catch                                                     
            try {
                resultSet.close();
                pt.close();
                connection.close();
            } // end try                                               
            catch (Exception exception) {
                exception.printStackTrace();
            } // end catch 
        } // end main`enter code here`
于 2013-10-08T10:45:08.243 回答