2016-10-29 38 views
0

我想要做的是通過表格視圖加載我的數據庫選擇一個項目並將其刪除到數據庫中。我沒有用戶輸入特定歌曲的ID,因此我很難完成此操作。我已經設置了GUI以及迄今爲止所有的代碼。通過JavaFX GUI從數據庫刪除數據

GUI代碼:enter image description here

SongContent代碼:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package playmymusic; 

import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 

/** 
* 
* @author man 
*/ 
public class SongContent 
{ 
    private final StringProperty artist; 
    private final StringProperty title; 
    private final StringProperty genre; 
    private final IntegerProperty id; 

    public SongContent(int id, String artist, String title, String genre) 
    { 
     this.artist = new SimpleStringProperty(artist); 
     this.title = new SimpleStringProperty(title); 
     this.genre = new SimpleStringProperty(genre); 
     this.id = new SimpleIntegerProperty(id); 
    } 

    public Integer getId() 
    { 
     return id.get(); 
    } 
    public void setID(int paramId) 
    { 
     id.set(paramId); 
    } 

    public String getArtist() 
    { 
     return artist.get(); 
    } 
    public void setArtist(String paramArtist) 
    { 
     artist.set(paramArtist); 
    } 

    public String getTitle() 
    { 
     return title.get(); 
    } 
    public void setTitle(String paramTitle) 
    { 
     title.set(paramTitle); 
    } 

    public String getGenre() 
    { 
     return genre.get(); 
    } 
    public void setGenre(String paramGenre) 
    { 
     genre.set(paramGenre); 
    } 

    public StringProperty artistProperty(){return artist;} 
    public StringProperty titleProperty(){return title;} 
    public StringProperty genreProperty(){return genre;} 
    public IntegerProperty idProperty() { return id;} 
} 

控制器代碼:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package playmymusic; 

import java.io.IOException; 
import java.net.URL; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ResourceBundle; 
import javafx.beans.property.IntegerProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Scene; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.TextField; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 
import javax.swing.JOptionPane; 
import org.apache.derby.jdbc.ClientDriver; 
/** 
* 
* @author man 
*/ 
public class FXMLDocumentController implements Initializable { 
    public LoginModel loginModel = new LoginModel(); 

    @FXML 
    private TextField txtUsername; 
    @FXML 
    private TextField txtPassword; 

    @FXML 
    private TextField txtArtist; 
    @FXML 
    private TextField fxTitle; 
    @FXML 
    private TextField fxGenre; 

    @FXML 
    private TableView<SongContent> tableView; 

    @FXML 
    private TableColumn<SongContent, Integer> id; 

    @FXML 
    private TableColumn<SongContent, String> artist; 
    @FXML 
    private TableColumn<SongContent, String> title; 
    @FXML 
    private TableColumn<SongContent, String> genre; 

    private ObservableList<SongContent> data; 


    @FXML 
    private void Login(ActionEvent event) throws SQLException { 
     try { 
      if(loginModel.isLogin(txtUsername.getText(), txtPassword.getText())) 
      { 
       Stage primaryStage = new Stage(); 
           FXMLLoader loader = new FXMLLoader(); 
       Pane root = loader.load(getClass().getResource("PopUpWindow.fxml").openStream()); 

       Scene scene = new Scene(root, 785, 809); 
       primaryStage.setScene(scene); 
       primaryStage.show(); 

       PlayMyMusic.primaryStage.close(); 
      }else 
      { 
          System.out.println("WOOPS"); 
         } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
       } 
    } 


    @FXML 
    private void songs(ActionEvent e) throws SQLException, ClassNotFoundException 
    { 

     loginModel.insertSongs(txtArtist.getText(), fxTitle.getText(), fxGenre.getText());  
     try 
     { 
      int i = 1; 
      Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/PlayMyMusicDB;user=test;password=test"); 
      data = FXCollections.observableArrayList(); 

      ResultSet rs = conn.createStatement().executeQuery("select * from Song"); 
      while(rs.next()) 
      { 
       data.add(new SongContent(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4))); 
       i++; 
      } 
     }catch(SQLException ex)  { 
      System.err.println("Error" + ex); 
     } 
     id.setCellValueFactory(new PropertyValueFactory<>("id")); 
     artist.setCellValueFactory(new PropertyValueFactory<>("artist")); 
     title.setCellValueFactory(new PropertyValueFactory<>("title")); 
     genre.setCellValueFactory(new PropertyValueFactory<>("genre")); 

     tableView.setItems(null); 
     tableView.setItems(data); 
     txtArtist.clear(); 
     fxTitle.clear(); 
     fxGenre.clear(); 

    } 

    @FXML 
    public void deleteItems(ActionEvent e) throws SQLException, ClassNotFoundException 
    { 
     Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/PlayMyMusicDB;user=test;password=test"); 
     int action = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this item?"); 
     if(action == 0) 
     { 
      try 
      { 
       IntegerProperty i = SongContent.idProperty(); 

       ResultSet rs = c.createStatement().executeQuery("DELETE FROM Song where i = " + i); 


      }catch(Exception e1) 
      { 
       e1.printStackTrace(); 
      } 
     } 
    } 


    @Override 
    public void initialize(URL url, ResourceBundle rb) 
    { 

    }  
} 
` 

爲什麼這不能被刪除我的數據的任何explination?如果有人向我解釋每次GUI打開和關閉時重置SongNumberID的策略,我也會喜歡它。但是,我的主要目標是弄清楚如何刪除歌曲。

非常感謝 -Aaron

+0

爲什麼地球上是'idProperty'靜態?當然,每首歌都有自己的ID? –

+0

是的,大聲笑我的錯誤我有它最初非靜態 – Aaron

+0

所以基本上,這將基本上解決這個問題,不是?將其設置爲非靜態,然後在'deleteItems()'方法中獲取所選歌曲的ID。 –

回答

1

SimpleIntegerProperty調用toString的結果是一樣的東西IntegerProperty [value: 10]。您應該使用該值,而不是IntegerProperty。此外,最好使用PreparedStatement來創建查詢。你也應該從表中獲取,而不是試圖引用一個實例方法,如果它是static所選項目:

SongContent song = tableView.getSelectionModel().getSelectedItem(); 
if (song != null) { 
    // there is a selection -> delete 
    ... 

    PreparedStatement statement = c.prepareStatement("DELETE FROM Song WHERE i = ?"); 
    statement.setInt(1, song.getId()); 
    statement.executeUpdate(); 
    ... 
} 

而且你應該確保i實際上是id列的列名(而不是id )。

+0

對不起,我忘記告訴大家,我找到了解決方案 – Aaron

+0

我最終刪除了用戶基於他們的藝術家,頭銜和流派的名字 – Aaron