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 4 of 4

Thread: Inserting data into sql with java gui

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Inserting data into sql with java gui

    [import java.awt.EventQueue;

    public class GUI {

    private JFrame frmResturant;
    public JTextField firstNameField;

    // Global Fields

    Connection con;
    Statement st;
    ResultSet rs;

    // Constructor for the class

    /**
    * Create the application.
    */

    public void connection() {
    /**
    * The Code Below allows me connect to the sql database Login is the
    * name of the database
    *
    */

    try {

    String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // driver string
    // stored in the
    // database
    Class.forName(driver);

    String db = "jdbcdbc:Login"; // db = database string stored in the
    // database
    con = DriverManager.getConnection(db);
    st = con.createStatement();

    } catch (Exception ex) {

    }

    }

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    GUI window = new GUI();
    window.frmResturant.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the application.
    */
    public GUI() {
    initialize();
    connection();
    initialize();
    }

    /**
    * Initialize the contents of the frame.
    */
    private void initialize() {
    frmResturant = new JFrame();
    frmResturant.getContentPane().setForeground(Color. WHITE);
    frmResturant.setForeground(Color.WHITE);
    frmResturant
    .setIconImage(Toolkit
    .getDefaultToolkit()
    .getImage(
    GUI.class
    .getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));
    frmResturant.setTitle(" Resturant");
    frmResturant.setResizable(false);
    frmResturant.setBounds(100, 100, 722, 341);
    frmResturant.setDefaultCloseOperation(JFrame.EXIT_ ON_CLOSE);
    frmResturant.getContentPane().setLayout(null);

    JLabel lblPleaseCompleteThe = new JLabel("Please Complete The Form ");
    lblPleaseCompleteThe.setForeground(new Color(0, 0, 255));
    lblPleaseCompleteThe.setBounds(290, 11, 158, 14);
    frmResturant.getContentPane().add(lblPleaseComplet eThe);

    JLabel lblFirstName = new JLabel("First Name :");
    lblFirstName.setBounds(10, 45, 72, 20);
    frmResturant.getContentPane().add(lblFirstName);

    firstNameField = new JTextField();
    firstNameField.setBounds(90, 45, 160, 20);
    frmResturant.getContentPane().add(firstNameField);
    firstNameField.setColumns(10);

    JButton btnButton = new JButton("button");
    btnButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

    try {

    String firstname = firstNameField.getText();

    String sql = "insert into login values UserName (' " +firstname + " ')";



    // Running the sql query
    rs = st.executeQuery(sql);

    int count = 0;
    while (rs.next()) {
    count = count + 1;
    }

    if (count == 1) {
    JOptionPane.showMessageDialog(null, "Welcome");
    } else if (count > 1) {
    JOptionPane.showMessageDialog(null,
    "Duplicate User Access Denied");
    }

    else {
    JOptionPane.showMessageDialog(null, " User Not Found ");
    }
    }

    catch (Exception ex) {

    }

    }
    });
    btnButton.setBounds(114, 99, 89, 23);
    frmResturant.getContentPane().add(btnButton);
    }
    }]


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Inserting data into sql with java gui

    Do you have a question or are you attempting to amaze us with your l33t codez?
    Improving the world one idiot at a time!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Inserting data into sql with java gui

    ...and please use code tags when posting code to the forum

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Inserting data into sql with java gui

    Hello.
    Type I JDBC driver does not work with applets/frames if i am correct.
    Try Type IV JDBC driver.

    Syed.

Similar Threads

  1. Reg - inserting json data to postgres db.
    By ramya in forum JDBC & Databases
    Replies: 2
    Last Post: August 27th, 2012, 01:34 PM
  2. Reg - inserting json data to postgres db using java
    By ramya in forum JDBC & Databases
    Replies: 2
    Last Post: August 27th, 2012, 12:50 PM
  3. Inserting into sql express using jdbc
    By tendaimare in forum JDBC & Databases
    Replies: 1
    Last Post: April 27th, 2012, 11:31 AM
  4. java.sql.SQLException: No data found
    By Virender in forum JDBC & Databases
    Replies: 2
    Last Post: December 7th, 2011, 01:17 PM
  5. [SOLVED] inserting utf8 data into DB
    By serdar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 5th, 2011, 05:21 PM