2017-07-11 2 views
1

私はまったく新しいjavaです。数週間前に始まったばかりです。私はすぐに完了しなければならない課題を持っています。この割り当てでは、ユーザーが適切なGUIを使用してデータベース操作を実行できるブックショップ用のJavaアプリケーションを作成する必要があると記載されています。Java SQLアプリケーション。入力が私のデータベースに更新されていない

このプロジェクトには3つのクラスが必要です。 1つはGETTERSとSETTERSのGUI要素用、もう1つはSQLデータベース用の準備文用です。

事前に、私はおそらく悪いコードの品質であることをお詫び申し上げます。

私の問題は、プロジェクトを実行して更新または挿入または削除しようとすると動作しないと思われますが、代わりにJOPtionPane「データが保存されません」というポップアップが表示されます。

私は本当に助け、ヒント、アドバイスをいただきありがとうございます。次のように私のプロジェクトのためのコードは次のとおりです。

BOOK CLASS(getterとsetter):今すぐ

package bookDatabase; 


public class Book { 
String bookName; // Create Variable for the book name// 
String authorName; 
String price; 

      //We now create the setters and getters 
      public Book(String bookName, String authorName, String price){ 
       this.bookName = bookName; 
       this.authorName = authorName; 
       this.price = price; 
       } 

      public void setbookName (String bookName){ 
       this.bookName = bookName; 
      } 

      public void setauthorName (String authorName){ 
       this.authorName = authorName; 
      } 

      public void setprice (String price){ 
       this.price = price; 
      } 

      public String getbookName(){ 
       return bookName; 
      } 

      public String getauthorName(){ 
       return authorName; 
      } 

      public String getprice(){ 
       return price; 
      } 



      @Override 
      public String toString(){ 
       return bookName + authorName + price ; 
      } 

      } 

guiBookクラス:

package bookDatabase; 

    import java.awt.*; 

import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.SQLException; 

public class guiBook extends JFrame{ 

private String bookName; 
private String authorName; 
private String price; 
Book b1 = new Book(bookName, authorName, price); 
BookQuerie q1 = new BookQuerie(bookName, authorName, price); 
private JTabbedPane pane = new JTabbedPane(); 
private JPanel panel1 = new JPanel(); 
private JTextArea areaShow= new JTextArea("List of All Books"); 
private JTable selectArea = new JTable(); 

private JPanel panel2 = new JPanel(); 
private JLabel update1 = new JLabel ("Insert Name of Book", JLabel.LEFT); 
private JTextField updateField1 = new JTextField(20); 
private JLabel update2 = new JLabel ("Enter the new Price", JLabel.LEFT); 
private JTextField updateField2 = new JTextField(20); 
private JButton updateButon = new JButton("Update price"); 


private JPanel panel3 = new JPanel(); 
private JTextField firstInsert = new JTextField(20); 
private JTextField secondInsert = new JTextField(20); 
private JTextField thirdInsert = new JTextField(20); 
private JButton insertButton = new JButton("Insert New Book"); 
private JLabel labelInsert1 = new JLabel("Enter Book Name", JLabel.LEFT); 
private JLabel labelInsert2 = new JLabel("Enter Author Name", JLabel.LEFT); 
private JLabel labelInsert3 = new JLabel("Enter Price"); 

private JPanel panel4 = new JPanel(); 
private JTextField firstDelete = new JTextField(20); 
private JButton buttonDelete = new JButton("Delete Book"); 
private JLabel labelDelete = new JLabel ("Enter Name of Book", JLabel.LEFT); 


public guiBook() throws Exception{ 
    pane.add("Display All Books", panel1); 
    panel1.add(areaShow, BorderLayout.WEST); 
    areaShow.setEditable(false); 
    panel1.add(selectArea, BorderLayout.EAST); 



    { 

    pane.add("Update Book Price", panel2); 
    panel2.add(update1); 
    panel2.add(updateField1); 
    panel2.add(update2); 
    panel2.add(updateField2); 
    panel2.add(updateButon); 
    updateButon.addActionListener(new ActionListener(){ 
    @Override 
    public void actionPerformed(ActionEvent e){ 
     String bookName = updateField1.getText(); 
     String price = updateField2.getText(); 
     try { 
      BookQuerie.updateBooks(bookName, price); 
     } 
     catch (SQLException e1) { 
      e1.printStackTrace(); 
     } 
    } 
    }); 

    pane.add("Insert New Book",panel3); 
    panel3.add(labelInsert1); 
    panel3.add(firstInsert); 
    panel3.add(labelInsert2); 
    panel3.add(secondInsert); 
    panel3.add(labelInsert3); 
    panel3.add(thirdInsert); 
    panel3.add(insertButton); 
    insertButton.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      String bookName = firstInsert.getText(); 
      String authorName = secondInsert.getText(); 
      String price = thirdInsert.getText(); 
      try { 
       BookQuerie.insertBooks(bookName,authorName, price); 
      } 
      catch (SQLException e1) { 
       e1.printStackTrace(); 
      } 
     } 
     }); 


    pane.add("Delete Book", panel4); 
    panel4.add(labelDelete); 
    panel4.add(firstDelete); 
    panel4.add(buttonDelete); 
    buttonDelete.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      String bookName = firstDelete.getText(); 
      try { 
       BookQuerie.deleteBooks(bookName); 
      } 
      catch (SQLException e1) { 
       e1.printStackTrace(); 
      } 
     } 
     }); 

    add(pane); 
    } 
} 


public static void main(String[] args) throws Exception{ 
    guiBook app = new guiBook(); 
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    app.setSize(1000,400); 
    app.setVisible(true); 
    } 


} 

そして最後に:BookQuerieクラス:

package bookDatabase; 

import java.sql.*; 

import javax.swing.JOptionPane; 
public class BookQuerie { 
private static PreparedStatement selectAll; 
private static PreparedStatement updateBook; 
private static PreparedStatement insertBook; 
private static PreparedStatement deleteBook; 
private Connection connect; 
private String bookName; 
private static String authorName; 
private static String price; 
Book boooks = new Book(bookName, authorName, price); 


public BookQuerie(String bookName, String authorName, String price) { 
    try{ 
     connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop", "root", ""); 
     selectAll = connect.prepareStatement("SELECT * FROM BOOK"); 
     updateBook = connect.prepareStatement("UPDATE book SET price = ? WHERE bookName = ?"); 
     insertBook = connect.prepareStatement("INSERT INTO book VALUES (?, ?, ?)"); 
     deleteBook= connect.prepareStatement("DELETE FROM book WHERE bookName = ? "); 
    } 
    catch 
     (SQLException ex){ 
      ex.printStackTrace(); 
     } 
    } 

public static String selectAll(String bookName, String authorName, String price) throws SQLException{ 
    ResultSet rs = selectAll.executeQuery(); 
    while(rs.next()){ 
     rs.getString ("bookName"); 
     rs.getString ("authorName"); 
     rs.getString ("price"); 
    } 
    return selectAll(bookName, authorName, price); 
} 

public static int updateBooks(String bookName, String price) throws SQLException{ 
    updateBook.setString(1, bookName); 
    updateBook.setString(2, price); 
    int i = updateBook.executeUpdate(); 
    if(i>0){ 
     JOptionPane.showMessageDialog(null, "Data Is Saved!!"); 
     System.exit(0); 
    } 
    else{ 
     JOptionPane.showMessageDialog(null, "Data is Not Saved! :("); 
     System.exit(0); 
    } 
    return updateBooks(bookName, price); 
} 

public static int insertBooks(String bookName, String authorName, String price) throws SQLException{ 
    insertBook.setString(1, bookName); 
    insertBook.setString(2,authorName); 
    insertBook.setString(3, price); 
    int i = insertBook.executeUpdate(); 
    if(i>0){ 
     JOptionPane.showMessageDialog(null, "Data Is Saved!!"); 
    } 
    else{ 
     JOptionPane.showMessageDialog(null, "Data is Not Saved! :("); 
    } 
    return insertBooks(bookName, authorName, price); 
} 

public static int deleteBooks (String bookName) throws SQLException{ 
    deleteBook.setString(1, bookName); 
    return deleteBook.executeUpdate(); 
} 


} 
+0

を持っています最初に宣言するのではなく、対応するメソッドの中でステートメントを準備しようとしましたか?手動でSQL GUIを使用して挿入しようとしましたか? – PSo

答えて

1

まず、 'Book'クラスの変数をプライベートにする必要があります。カプセル化はOOPの重要な概念です。

あなたも例えば、あなたのINSERT文で明示的にデータベースの列に名前を付けてみてください:

文字列のクエリ= "ユーザー(FIRST_NAME、LAST_NAME、DATE_CREATED、is_admin、となる。num_points)値(挿入、、??? 、?、?) ";

第2に、guiBook(GuiBookまたはGUIBookのクラス名は大文字で始める必要があります)では、作成したブッククラスを使用します。 テキストフィールドから取得した値からブックオブジェクトを作成し、 '挿入'メソッドに渡します。

insertButton.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      String bookName = firstInsert.getText(); 
      String authorName = secondInsert.getText(); 
      String price = thirdInsert.getText(); 
      Book book = new Book(bookName,authorName, price): 
      try { 
       BookQuerie.insertBooks(book); 
      } 
      catch (SQLException e1) { 
       e1.printStackTrace(); 
      } 
     } 
     }); 

最後に、私はコンストラクタで準備された文のすべてを作成しないためにBookQuerie(クエリ)クラスを変更することをお勧めします。代わりに、あなたが達成したい各データベース・アクションのための個々のメソッドを作成する例:

public static boolean insertBook(Book book) throws SQLException{ 
PreparedStatement insertBook = null; 
    try(Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop", "root", "");){ 

     insertBook = connect.prepareStatement("INSERT INTO book (NAME, AUTHOR, PRICE) VALUES (?, ?, ?)"); 


     insertBook.setString(1, book.getName()); 
     insertBook.setString(2,book.getAuthorName()); 
     insertBook.setString(3, book.getPrice()); 
     int i = insertBook.executeUpdate(); 
     if(i>0){ 
      JOptionPane.showMessageDialog(null, "Data Is Saved!!"); 
    return true; 
     } 
     else{ 
      JOptionPane.showMessageDialog(null, "Data is Not Saved! :("); 
return false; 
     } 
    catch 
      (SQLException ex){ 
       ex.printStackTrace(); 
      } 
finally{ 
      insertBook.close(); 
      } 
    } 

注:試してみる(接続CONN)内部の接続を作成する{}「のリソースとしてみてください」と呼ばれるが、それは以降のJava 7から提供されています最後に接続を自動的に閉じます。私が述べたことはありません

EDIT

ことの一つは、例えば自分自身を返すあなたの方法でした

public static List<Book> selectAll() throws SQLException { 
     **return selectAll();** 
    } 

これは「再帰」と呼ばれ、問題を引き起こす可能性があります。あなたのコードでは、とにかくそこにあるべきではありません。

selectAll()メソッドに関しては、DBから複数のレコードを選択していますので、これらはある種のリストに格納する必要があります。例えば:

public static List<Book> selectAll() throws SQLException { 
     PreparedStatement ps = null; 
     ResultSet rs = null; 
     // THE LIST OF BOOKS YOU WILL RETURN 
     List<Book> books = new ArrayList<>(); 
     String sql = "SELECT NAME, AUTHOR, PRICE FROM BOOK"; 

     try(Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop", "root", "");){ 

      ps = conn.prepareStatement(sql); 
      rs = ps.executeQuery(); 

      while(rs.next()){ 
       String name = rs.getString ("NAME"); 
       String author = rs.getString ("AUTHOR"); 
       String price = rs.getString ("PRICE"); 
       // CREATE NEW BOOK WITH EACH ROW FROM RESULTSET 
       Book book = new Book(name, author, price); 
       // ADD BOOK TO THE LIST 
       books.add(book); 
      } 
     } 
     finally{ 
      ps.close(); 
      rs.close(); 
     } 
     // RETURN LIST OF ALL BOOKS 
     return books; 
    } 

その後、あなたのGuiBookクラスであなたがたとえば、あなたのJPanelを移入するためのSelectAll()メソッドを使用する別の方法で作成することができます。

private void populateJPanel(){ 
     // GET LIST OF ALL BOOKS (selectAll() throws sql exception so we handle it here.) 
     List<Book> books = null; 
     try { 
      books = BookQuery.selectAll(); 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
// LOOP THROUGH BOOK LIST 
      for (Book book : books) { 
       String name = book.getName(); 
       String author = book.getAuthor(); 
       String price = book.getPrice(); 

       // NOW USE THESE VALUES HOWEVER YOU PLEASE ON YOUR JPANEL 
      } 
     } 

・ホープこのことができますが...

+0

ありがとうbadgerboy86!これはうまくいきました。私は助けに感謝します。乾杯! –

+0

あなたは大丈夫ですか? upvoteと正しい答えとしてマークすることを忘れないでください... – badgerboy86

+0

badgerboy86、私はまだ私のGUIクラスにすべてのクエリを選択して印刷するのに苦労しています。私はそれをJTableに印刷しようとしていますが、私は何もしていないようです。これを行う方法に関するアドバイス?私もあなたのためにアップしましたが、私のREPが15歳未満であるので、それは公に示されていません。私は正解とマークした。乾杯。 –

関連する問題