如何将数据从jList传递到数据库(mySQL)

时间:2015-12-07 13:06:56

标签: java mysql sql netbeans jlist

我需要一些关于我的计划的帮助。如何将数据从JList传递到数据库?

在ItemDetails.java中,我已将复选框值传递给另一个JForm中的JList。现在我想将数据从JList检索到我的数据库中。

ItemDetails.java

    private void jButtonNextActionPerformed(java.awt.event.ActionEvent evt) {                                            

    ShoppingCart sc=new ShoppingCart();
             if(!jCheckBoxAdele.isSelected()&&!jCheckBox1DPerfect.isSelected()&&!jCheckBoxBieber.isSelected()
            &&!jCheckBox1D1Thing.isSelected()&&!jCheckBoxHujan.isSelected()&&!jCheckBoxSamWriting.isSelected()
            &&!jCheckBoxAlessiaWil.isSelected()&&!jCheckBoxSamStayWithMe.isSelected()
            &&!jCheckBoxFx.isSelected()&&!jCheckBoxSonaone.isSelected()&&!jCheckBoxRabbani.isSelected()
            &&!jCheckBoxTroye.isSelected()&&!jCheckBoxMarvinGaye.isSelected())
    {

      sc.setVisible(false);

     JOptionPane.showMessageDialog(null, "Please pick your song !!!");
     dispose();
     new ItemDetails().setVisible(true);
    }


    if(jCheckBoxAdele.isSelected())
        songsdetails.add("Adele - Hello");

    if(jCheckBox1DPerfect.isSelected())
        songsdetails.add("One Direction - Perfect");

    if(jCheckBoxBieber.isSelected())
        songsdetails.add("Justin Bieber - Sorry");

    if(jCheckBox1D1Thing.isSelected())
        songsdetails.add("One Direction - One Thing");

    if(jCheckBoxHujan.isSelected())
        songsdetails.add("Hujan - Anging Kencang");

    if(jCheckBoxSamWriting.isSelected())
        songsdetails.add("Sam Smith - Writing On The Walls");

    if(jCheckBoxAlessiaWil.isSelected())
        songsdetails.add("Alessia Cara");

    if(jCheckBoxSamStayWithMe.isSelected())
        songsdetails.add("Sam Smith - Stay With Me");

    if(jCheckBoxFx.isSelected())
        songsdetails.add("F(x) - 4 Walls");

    if(jCheckBoxDemi.isSelected())
        songsdetails.add("Demi Lovato - I Really Don't Care");

    if(jCheckBoxSonaone.isSelected())
        songsdetails.add("Sonaone - Firefly");

    if(jCheckBoxRabbani.isSelected())
        songsdetails.add("Rabbani - Pergi Tak Kembali");

    if(jCheckBoxTroye.isSelected())
        songsdetails.add("Troye Sivan - Fools");

    if(jCheckBoxMarvinGaye.isSelected())
        songsdetails.add("Charlie Puth - Marvin Gaye");

        new ShoppingCart(songsdetails).setVisible(true);
        dispose(); 

}         

ShoppingCart.java

public class ShoppingCart extends javax.swing.JFrame {
public Connection cn;
public PreparedStatement st;
static ArrayList songsdetails;

/**
 * Creates new form ShoppingCart
 */
public ShoppingCart() {
    initComponents();
}

public ShoppingCart(ArrayList SONGSDETAILS) {
    initComponents();
    songsdetails = SONGSDETAILS;
}               
private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {                                            

    this.setVisible(false);
    jList1.clearSelection();
    new ItemDetails().setVisible(true);

}                                           

private void jButtonNextActionPerformed(java.awt.event.ActionEvent evt) {                                            


        try {
            Class.forName("com.mysql.jdbc.Driver");

            cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sdmusic","root","");

            st=cn.prepareStatement("SELECT `Username`, `SongsSelection` FROM `user` WHERE `Username`=?  'SongsSelection' = ?");





        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ShoppingCart.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
        Logger.getLogger(ShoppingCart.class.getName()).log(Level.SEVERE, null, ex);
    }

   dispose();
   new CalculatePay().setVisible(true);

}                                           

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    // TODO add your handling code here:
    DefaultListModel list = new DefaultListModel();
    System.out.println(""+songsdetails.get(0));
    for(int i=0; i <songsdetails.size();i++){
        list.addElement(songsdetails.get(i));
    }
    jList1.setModel(list);


}                                 

1 个答案:

答案 0 :(得分:0)

  1. 从您的JList see here
  2. 中检索所需的值
  3. 准备INSERT INTO声明,see here
  4. 执行您的陈述see here