Results 1 to 2 of 2
  1. #1
    kwink is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Unhappy Help Pls!! Jcombobox populate with mysql

    Hi guys! need help, I don't know how to populate my combobox linking it to mysql.. i've read alot of topics regarding this but i still couldn't get it.. Im a slow learner and a beginner in Java. I'm using netbeans.
    if there is no way of linking my combobox to mysql, could anyone pls tell me how to save the items i added in the combobox even when I close the window. cause when I run the program, whenever I close the window or end the program the items added in my combobox would be gone.. (the letters in color red in my code is the one)T_T HELP!!! :confused:
    this is my code..

    /*
    * MainClass.java
    *
    * Created on March 13, 2009, 3:37 PM
    */

    /**
    *
    * @author windemuy
    */
    import java.sql.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;

    public class MainClass extends javax.swing.JFrame {

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

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    jTabbedPane1 = new javax.swing.JTabbedPane();
    jPanel1 = new javax.swing.JPanel();
    jTabbedPane4 = new javax.swing.JTabbedPane();
    jPanel2 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    txtname = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    address = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    txtcontact = new javax.swing.JTextField();
    cbobook = new javax.swing.JComboBox();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    txtremarks = new javax.swing.JTextField();
    jLabel7 = new javax.swing.JLabel();
    txtpayment = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jLabel11 = new javax.swing.JLabel();
    txtid = new javax.swing.JTextField();
    jLabel12 = new javax.swing.JLabel();
    txtprice = new javax.swing.JTextField();
    txtchange = new javax.swing.JTextField();
    jButton5 = new javax.swing.JButton();
    jPanel3 = new javax.swing.JPanel();
    jLabel8 = new javax.swing.JLabel();
    txtadd = new javax.swing.JTextField();
    jLabel9 = new javax.swing.JLabel();
    txtremove = new javax.swing.JTextField();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jLabel10 = new javax.swing.JLabel();
    num = new javax.swing.JTextField();
    jPanel5 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();
    jButton4 = new javax.swing.JButton();
    jButton6 = new javax.swing.JButton();
    jPanel6 = new javax.swing.JPanel();
    jLabel13 = new javax.swing.JLabel();
    txtname1 = new javax.swing.JTextField();
    jLabel14 = new javax.swing.JLabel();
    address1 = new javax.swing.JTextField();
    jLabel15 = new javax.swing.JLabel();
    txtcontact1 = new javax.swing.JTextField();
    cbobook1 = new javax.swing.JComboBox();
    jLabel16 = new javax.swing.JLabel();
    jLabel17 = new javax.swing.JLabel();
    txtremarks1 = new javax.swing.JTextField();
    jLabel18 = new javax.swing.JLabel();
    txtpayment1 = new javax.swing.JTextField();
    jButton7 = new javax.swing.JButton();
    jLabel19 = new javax.swing.JLabel();
    txtid1 = new javax.swing.JTextField();
    jLabel20 = new javax.swing.JLabel();
    txtprice1 = new javax.swing.JTextField();
    txtchange1 = new javax.swing.JTextField();
    jButton8 = new javax.swing.JButton();

    getContentPane().setLayout(null);

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setTitle("Winde's Book Shop");
    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowOpened(java.awt.event.WindowEvent evt) {
    window(evt);
    }
    });

    jLabel1.setText("Winde's Book Shop");
    getContentPane().add(jLabel1);
    jLabel1.setBounds(250, 30, 140, 14);

    jPanel1.setLayout(null);

    jTabbedPane4.setTabPlacement(javax.swing.JTabbedPa ne.BOTTOM);
    jPanel2.setLayout(null);

    jLabel2.setText("Name:");
    jPanel2.add(jLabel2);
    jLabel2.setBounds(30, 80, 60, 20);

    jPanel2.add(txtname);
    txtname.setBounds(100, 80, 380, 20);

    jLabel3.setText("Address:");
    jPanel2.add(jLabel3);
    jLabel3.setBounds(30, 110, 60, 14);

    jPanel2.add(address);
    address.setBounds(100, 110, 380, 20);

    jLabel4.setText("Contact:");
    jPanel2.add(jLabel4);
    jLabel4.setBounds(30, 140, 60, 14);

    jPanel2.add(txtcontact);
    txtcontact.setBounds(100, 140, 380, 20);

    jPanel2.add(cbobook);
    cbobook.setBounds(100, 190, 380, 22);

    jLabel5.setText("Book Title");
    jPanel2.add(jLabel5);
    jLabel5.setBounds(30, 200, 60, 14);

    jLabel6.setText("Remarks");
    jPanel2.add(jLabel6);
    jLabel6.setBounds(30, 240, 100, 14);

    jPanel2.add(txtremarks);
    txtremarks.setBounds(100, 240, 380, 20);

    jLabel7.setText("Payment:");
    jPanel2.add(jLabel7);
    jLabel7.setBounds(270, 280, 60, 14);

    jPanel2.add(txtpayment);
    txtpayment.setBounds(340, 280, 80, 20);

    jButton1.setText("Submit");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Submit(evt);
    }
    });

    jPanel2.add(jButton1);
    jButton1.setBounds(160, 360, 200, 40);

    jLabel11.setText("Customer ID");
    jPanel2.add(jLabel11);
    jLabel11.setBounds(30, 40, 80, 14);

    jPanel2.add(txtid);
    txtid.setBounds(100, 30, 70, 30);

    jLabel12.setText("Book Price");
    jPanel2.add(jLabel12);
    jLabel12.setBounds(30, 280, 70, 14);

    jPanel2.add(txtprice);
    txtprice.setBounds(120, 280, 70, 20);

    txtchange.setEditable(false);
    jPanel2.add(txtchange);
    txtchange.setBounds(340, 310, 80, 20);

    jButton5.setText("Change");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Change(evt);
    }
    });

    jPanel2.add(jButton5);
    jButton5.setBounds(240, 310, 90, 23);

    jTabbedPane4.addTab("Customer", jPanel2);

    jPanel3.setLayout(null);

    jLabel8.setText("Add Title");
    jPanel3.add(jLabel8);
    jLabel8.setBounds(60, 80, 80, 14);

    jPanel3.add(txtadd);
    txtadd.setBounds(140, 80, 210, 20);

    jLabel9.setText("Remove Title");
    jPanel3.add(jLabel9);
    jLabel9.setBounds(60, 120, 80, 14);

    jPanel3.add(txtremove);
    txtremove.setBounds(140, 120, 210, 20);

    jButton2.setText("Add");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Add(evt);
    }
    });

    jPanel3.add(jButton2);
    jButton2.setBounds(380, 80, 100, 23);

    jButton3.setText("Remove");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Remove(evt);
    }
    });

    jPanel3.add(jButton3);
    jButton3.setBounds(380, 120, 100, 23);

    jLabel10.setText("Current Books Available:");
    jPanel3.add(jLabel10);
    jLabel10.setBounds(100, 200, 170, 20);

    jPanel3.add(num);
    num.setBounds(250, 190, 50, 30);

    jTabbedPane4.addTab("Book", jPanel3);

    jPanel1.add(jTabbedPane4);
    jTabbedPane4.setBounds(30, 20, 520, 440);

    jTabbedPane1.addTab("Add +", jPanel1);

    jPanel5.setLayout(null);

    jTable1.setBackground(new java.awt.Color(255, 204, 153));
    jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
    {null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null},
    {null, null, null, null, null, null, null}
    },
    new String [] {
    "ID", "Name", "Address", "Contact", "Title", "Remarks", "Price"
    }
    ));
    jScrollPane1.setViewportView(jTable1);

    jPanel5.add(jScrollPane1);
    jScrollPane1.setBounds(30, 40, 530, 270);

    jButton4.setText("Load Data");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Load(evt);
    }
    });

    jPanel5.add(jButton4);
    jButton4.setBounds(120, 340, 130, 23);

    jButton6.setText("Delete");
    jButton6.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Delete(evt);
    }
    });

    jPanel5.add(jButton6);
    jButton6.setBounds(350, 340, 100, 23);

    jTabbedPane1.addTab("View", jPanel5);

    jPanel6.setLayout(null);

    jLabel13.setText("Name:");
    jPanel6.add(jLabel13);
    jLabel13.setBounds(60, 90, 60, 20);

    jPanel6.add(txtname1);
    txtname1.setBounds(130, 90, 380, 20);

    jLabel14.setText("Address:");
    jPanel6.add(jLabel14);
    jLabel14.setBounds(60, 120, 60, 14);

    jPanel6.add(address1);
    address1.setBounds(130, 120, 380, 20);

    jLabel15.setText("Contact:");
    jPanel6.add(jLabel15);
    jLabel15.setBounds(60, 150, 60, 14);

    jPanel6.add(txtcontact1);
    txtcontact1.setBounds(130, 150, 380, 20);

    cbobook1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Harry Potter", "Spiderwick", "Angels & Demons", "Da Vinci Code", "Barbie" }));
    jPanel6.add(cbobook1);
    cbobook1.setBounds(130, 200, 380, 22);

    jLabel16.setText("Book Title");
    jPanel6.add(jLabel16);
    jLabel16.setBounds(60, 210, 60, 14);

    jLabel17.setText("Remarks");
    jPanel6.add(jLabel17);
    jLabel17.setBounds(60, 250, 100, 14);

    jPanel6.add(txtremarks1);
    txtremarks1.setBounds(130, 250, 380, 20);

    jLabel18.setText("Payment:");
    jPanel6.add(jLabel18);
    jLabel18.setBounds(300, 290, 60, 14);

    jPanel6.add(txtpayment1);
    txtpayment1.setBounds(360, 290, 80, 40);

    jButton7.setText("Update");
    jButton7.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Update(evt);
    }
    });

    jPanel6.add(jButton7);
    jButton7.setBounds(240, 390, 100, 23);

    jLabel19.setText("Customer ID");
    jPanel6.add(jLabel19);
    jLabel19.setBounds(60, 50, 80, 14);

    jPanel6.add(txtid1);
    txtid1.setBounds(130, 40, 70, 30);

    jLabel20.setText("Book Price");
    jPanel6.add(jLabel20);
    jLabel20.setBounds(60, 290, 70, 14);

    jPanel6.add(txtprice1);
    txtprice1.setBounds(150, 290, 70, 20);

    txtchange1.setEditable(false);
    jPanel6.add(txtchange1);
    txtchange1.setBounds(150, 330, 70, 20);

    jButton8.setText("Change");
    jButton8.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Change2(evt);
    }
    });

    jPanel6.add(jButton8);
    jButton8.setBounds(50, 330, 90, 23);

    jTabbedPane1.addTab("Update *", jPanel6);

    getContentPane().add(jTabbedPane1);
    jTabbedPane1.setBounds(20, 60, 580, 510);

    pack();
    }
    // </editor-fold>

    private void Update(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    Connection conn = null;
    int rows=0;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost/books_db","root", "root");
    Statement s = conn.createStatement();
    rows = s.executeUpdate("Update books set Name='"+txtname1.getText() +"',Address='"+address1.getText() +"',Contact='"+
    txtcontact1.getText()+"',Title='"+cbobook1.getSele ctedItem()+"',Remarks='"+txtremarks1.getText()+"', Price='"+txtprice1.getText()+"' where ID='"+txtid1.getText()+"'");
    conn.close();
    }catch(Exception ex){
    JOptionPane.showMessageDialog(this,ex.getMessage() );
    }

    if(rows>0)
    JOptionPane.showMessageDialog(this,"Record has been updated");
    }

    private void Change2(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    int first = Integer.parseInt(txtpayment1.getText());
    int second = Integer.parseInt(txtprice1.getText());
    int answer = first - second;
    txtchange1.setText(" " + answer);
    }

    private void Submit(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    Connection conn=null;

    try {
    Class.forName("com.mysql.jdbc.Driver");
    conn=DriverManager.getConnection("jdbc:mysql://localhost/books_db","root","root");
    Statement st=conn.createStatement();
    int rows=st.executeUpdate("Insert into books(ID,Name,Address,Contact,Title,Remarks,Price) " +
    " values("+ txtid.getText()+",'"+ txtname.getText()+"','"+ address.getText()+"','"+ txtcontact.getText()+"','"+ cbobook.getSelectedItem()+"','"+ txtremarks.getText()+"','"+ txtprice.getText()+"');");

    conn.close();
    }catch(Exception ex){

    JOptionPane.showMessageDialog(this,ex.getMessage() );

    }
    }

    private void Change(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    int first = Integer.parseInt(txtpayment.getText());
    int second = Integer.parseInt(txtprice.getText());
    int answer = first - second;
    txtchange.setText(" " + answer);
    }
    //this is the part where i am supposed to populate my combo box.. no codes available in my head... :confused: HELP
    private void Add(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }


    private void Remove(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    int index=-1;
    try{
    index = Integer.parseInt(txtremove.getText());
    txtremove.setText("");
    cbobook.removeItemAt(index);
    } catch (Exception e){
    javax.swing.JOptionPane.showMessageDialog(this, e.getMessage());
    return;
    }
    int nitems = cbobook.getItemCount();
    num.setText(" " + nitems + "\n");
    }

    private void Load(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    Connection conn = null;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost/books_db","root","root");
    Statement s=conn.createStatement();
    ResultSet rs = s.executeQuery("Select * from books");
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
    model.setNumRows(0);
    while (rs.next()){
    String []rows = new String[7];
    rows[0] = rs.getString("ID");
    rows[1] = rs.getString("Name");
    rows[2] = rs.getString("Address");
    rows[3] = rs.getString("Contact");
    rows[4] = rs.getString("Title");
    rows[5] = rs.getString("Remarks");
    rows[6] = rs.getString("Price");
    model.addRow(rows);
    }
    conn.close();
    }catch(Exception e){
    JOptionPane.showMessageDialog(this,e.getMessage()) ;
    }
    }

    private void Delete(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    if(jTable1.getSelectedRow()==-1){
    JOptionPane.showMessageDialog(this,"Select a row to be deleted.");
    return;
    }
    Connection conn=null;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost/books_db","root","root");
    Statement st = conn.createStatement();
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
    String pid = (String)model.getValueAt(jTable1.getSelectedRow(), 0);
    st.executeUpdate("Delete from books where ID =" +pid);
    conn.close();
    } catch(Exception e){
    JOptionPane.showMessageDialog(this, e.getMessage());
    }JOptionPane.showMessageDialog(this,"Row deleted, click Load Data to Refresh Table.");
    }

    private void window(java.awt.event.WindowEvent evt) {
    // TODO add your handling code here:
    setSize(630,630);
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    int screenwidth = toolkit.getScreenSize().width;
    int screenheight = toolkit.getScreenSize().height;
    int x = (screenwidth/2) - (this.getSize().width/2);
    int y = (screenheight/2) - (this.getSize().height/2);
    setLocation(x, y);


    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new MainClass().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JTextField address;
    private javax.swing.JTextField address1;
    private javax.swing.JComboBox cbobook;
    private javax.swing.JComboBox cbobook1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField num;
    private javax.swing.JTextField txtadd;
    private javax.swing.JTextField txtchange;
    private javax.swing.JTextField txtchange1;
    private javax.swing.JTextField txtcontact;
    private javax.swing.JTextField txtcontact1;
    private javax.swing.JTextField txtid;
    private javax.swing.JTextField txtid1;
    private javax.swing.JTextField txtname;
    private javax.swing.JTextField txtname1;
    private javax.swing.JTextField txtpayment;
    private javax.swing.JTextField txtpayment1;
    private javax.swing.JTextField txtprice;
    private javax.swing.JTextField txtprice1;
    private javax.swing.JTextField txtremarks;
    private javax.swing.JTextField txtremarks1;
    private javax.swing.JTextField txtremove;
    // End of variables declaration

    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    just like your Update method, you need to connect to your database and execute 'select' queue to get data from database, get data from ResultSet and add to combobox

Similar Threads

  1. populate jCombobox with database data
    By joeyxaza in forum JDBC
    Replies: 2
    Last Post: 01-06-2014, 10:09 AM
  2. populate jtable from jtextboxes and jcombo
    By purejoker in forum AWT / Swing
    Replies: 4
    Last Post: 12-02-2010, 09:29 AM
  3. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM
  4. populate jCombobox with database data
    By joeyxaza in forum JDBC
    Replies: 0
    Last Post: 01-19-2009, 04:30 PM
  5. how to populate data in drop-down box
    By ma-la in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-01-2008, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •