Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JCombobox connecting to mysql

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JCombobox connecting to mysql

    Hello, I'm new in java.

    How I can connect mysql to JCombobox?

    I already researched it but I really don't understand.


    My JCombobox is inventcateg
    my table is category
    the column that I need in my JCombobox is categoryNo

    here's my code:
    I don't know where I will be putting codes to connect my JCombobox to mysql. Please help me.

    package aaaaa;
    import java.sql.*;
     
    public class InventoryS extends javax.swing.JFrame {
     
        private Connection connect;
     
        public InventoryS() {
            initComponents();
     
            try{
                Class.forName("com.mysql.jdbc.Driver");
                connect = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/aaaaa","root","");
                statusTextArea.setText("Connection SUCCESSFUL!");
            }
            catch (Exception f)
            {statusTextArea.setText(f.getMessage());
     
            }
        }
     
     
        @SuppressWarnings("unchecked")
     
     private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
            try
            {
                connect.close();
            }
            catch(Exception f){};
        }                                  
     
        private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt) {
            String inventcodef = inventCodeField.getText();
            String inventnamef = inventNameField.getText();
    	String inventcateg = "Select categoryNo from category order by categoryNo;
            int inventreorderf = Integer.parseInt(inventReorderField.getText());
            String inventlocf = inventLocationField.getText();
            String inventstat = inventStatusArea.getText();
     
            try {
                String SQL2 = "INSERT INTO inventory VALUES ("
                        + "'" + inventcodef + "'" + ","
                        + "'" + inventnamef + "'" + ","
                        + "'" + inventreorderf + "'" + ","
                        + "'" + inventlocf + "'" + ","
                        + "'" + inventstat + "'"
                        + ")";
     
                PreparedStatement stamt = connect.prepareStatement(SQL2);
                stamt.executeUpdate();
     
                statusTextArea.setText("Successfully saved!");
            } catch (Exception f) {
                statusTextArea.setText(f.getMessage());
     
            }
        }
     
     
        public static void main(String args[]) {
     
           java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new InventoryS().setVisible(true);
                }
            });
        }

    Thanks in advance!
    Last edited by myexternall11; February 16th, 2012 at 11:49 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: JCombobox connecting to mysql

    Please use the code tags, and please define what you mean by 'connect them'.

  3. The Following User Says Thank You to copeg For This Useful Post:

    myexternall11 (February 16th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JCombobox connecting to mysql

    What I mean for "connecting" is retrieving my data from MYSQL and place them into the JCOMBOBOX then whatever the user choose it will be saved
    Thanks in advance
    Last edited by myexternall11; February 16th, 2012 at 10:19 PM.

Similar Threads

  1. Connecting to VPN
    By raviteja84 in forum Java Networking
    Replies: 0
    Last Post: October 18th, 2011, 05:53 PM
  2. Replies: 6
    Last Post: September 5th, 2011, 10:02 AM
  3. JComboBox
    By SV25 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 20th, 2011, 08:47 AM
  4. [SOLVED] JCombobox help
    By sman36 in forum AWT / Java Swing
    Replies: 10
    Last Post: August 11th, 2010, 04:11 AM
  5. JComboBox
    By nasi in forum AWT / Java Swing
    Replies: 1
    Last Post: April 29th, 2010, 08:40 AM