0

Tomcat を実行しているデータベースに接続する Java プログラムがあります。アプリには、名、姓、電子メール、電話のフィールドが含まれています。クリックすると、テキスト フィールドのエントリをデータベースに追加できるボタンを作成しました。

以下に、エントリを追加する構造を示しました。クライアント エントリを削除するのと同じ方法を使用します。問題は SQL コマンドです。書き方がわかりません。

質問: (クライアントの追加など) データベースからデータをフィールドにロードし、その情報を取得してデータベース内の特定のエントリを削除できる SQL コマンドが必要です。助けてください。

クライアントの挿入 (クエリ クラス内):

//create INSERT that adds a new entry into the database
            insertNewPerson = connection.prepareStatement(
                    "INSERT INTO Addresses " + 
                    "(FirstName, LastName, Email, PhoneNumber ) " +
                    "VALUES (?, ?, ?, ?)" );

人物を追加する方法 (Queries クラス):

//ADD an entry
    public int addPerson(
            String fname, String lname, String email, String num)
    {
        int result = 0;

        //set parameters, then execute insertNewPerson
        try {
            insertNewPerson.setString(1, fname);
            insertNewPerson.setString(2, lname);
            insertNewPerson.setString(3, email);
            insertNewPerson.setString(4, num);

            //insert the new entry; return # of rows updated
            result = insertNewPerson.executeUpdate();
        }//end try
        catch(SQLException sqlException) {
            sqlException.printStackTrace();
            close();
        }//end catch

        return result;
    }//end method addPerson

実行されたアクション (アプリケーション クラスおよび GUI を使用):

//handles call when insertButton is clicked
            private void insertButtonActionPerformed(ActionEvent evt)
            {
                int result = personQueries.addPerson(firstNameTextField.getText(), lastNameTextField.getText(), emailTextField.getText(), phoneTextField.getText());

                if (result == 1)
                    JOptionPane.showMessageDialog(this,"Person added!", "Person added", JOptionPane.PLAIN_MESSAGE);
                else
                    JOptionPane.showMessageDialog(this, "Person not added!", "Error", JOptionPane.PLAIN_MESSAGE);


                browseButtonActionPerformed(evt);
            }//end method insertButtonActionPerformed
4

1 に答える 1

1

私があなたを正しく理解していれば:

DELETE FROM
    Addresses
WHERE
    FirstName = <Your value here> AND
    LastName = <Your value here> AND
    Email = <Your value here> AND
    PhoneNumber = <Your value here>

Addressesこれにより、すべての条件が真の場所から削除されます。

より洗練された解決策は、主キーを持つ行を削除することでしょう。

于 2013-05-14T19:43:44.133 に答える