3

I have an sql table: User, with one field: NAME.

I created a popup dialog window, where I should type in the Users's name, after that, it should be passed to the column NAME in mysql. So, it is pretty simple but me as beginner got stuck with some error. Is there a way to fix my code to work smoothly?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.*;

public class Userdialog {

static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/mydb";
static final String USER="root";
static final String PASS="";

Connection conn = null;
Statement st = null;

JTextField name;
JButton proceed;

Userdialog(){
    JFrame useradd = new JFrame("Add user to database");
    useradd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    name = new JTextField(10);
    useradd.setLayout(new GridLayout(0,1));
    JPanel pane = new JPanel(new GridLayout(0,1));
    useradd.add(pane);
    pane.add(new JLabel("First name: "));
    pane.add(name);
    proceed = new JButton("Add that user");
    pane.add(proceed);
    proceed.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //HOW TO MAKE IT TO THE SQL TABLE???
            try
    {
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection(DB_URL,USER,PASS);
        st = (Statement) conn.createStatement();
        st.execute("insert into user (name) values('Philip')");
        JOptionPane.showMessageDialog(null,"Inserted Successfully!");
    }
    catch(Exception ex) {  ex.printStackTrace();   }
        }});
    useradd.pack();
    useradd.setSize(300,200);
    useradd.setVisible(true);
}}

MAIN CLASS

public class Main {
    public static void main(String[] args) {
        Userdialog mc = new Userdialog();
    }}
2
  • Could you provide us with the error that you receive Commented Apr 17, 2014 at 11:02
  • No more errors, I fiexed the code. But something's wrong. Try it and compile to see, I got an exception insted a new row in my database :( Commented Apr 17, 2014 at 11:24

1 Answer 1

2

With your code alone i see a few issues:

static final String MYNAME = JTextField.getText();

Will throw a exception also I see you don't use it anywhere so you can throw that away.

Also if you ahev the mysql library jar then you importing the wrong

import java.beans.Statement;

Take that out you want to rather use the mysql statement instead:

import com.mysql.jdbc.Statement;

So far try those changes i am going to try and compile your code and see what I can get.

EDIT1***

Also small tip when catching a exception its always considered good practice to log the exception in your case the following will be sufficient:

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

EDIT2***

here is a working version of your code sorry ti took so long:

import com.mysql.jdbc.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

public class Userdialog {

  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  static final String DB_URL      = "jdbc:mysql://localhost/mydb";
  static final String USER        = "root";
  static final String PASS        = "";

  Connection conn = null;
  Statement  st   = null;

  JTextField name;
  JButton    proceed;

  public Userdialog()
  {
    JFrame useradd = new JFrame("Add user to database");
    useradd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    name = new JTextField(10);
    useradd.setLayout(new GridLayout(0, 1));
    JPanel pane = new JPanel(new GridLayout(0, 1));
    useradd.add(pane);
    pane.add(new JLabel("First name: "));
    pane.add(name);
    proceed = new JButton("Add that user");
    pane.add(proceed);
    proceed.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        //HOW TO MAKE IT TO THE SQL TABLE???
        try
        {
          Class.forName("com.mysql.jdbc.Driver");
          conn= DriverManager.getConnection(DB_URL, USER, PASS);
          st = (Statement) conn.createStatement();
          st.execute("insert into user (name) values('"+name.getText()+"')");
          JOptionPane.showMessageDialog(null, "Inserted Successfully!");
        }
        catch(Exception ex)
        {
          ex.printStackTrace();
        }
      }});
    useradd.pack();
    useradd.setSize(300,200);
    useradd.setVisible(true);
  }

  public static void main(String[] args)
  {
    new Userdialog();
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I updated the code above with yours suggestions. I got an exeption. No error anymore, but when I execute it and insert the text in the textfield it throw an exception. Try it please. I got nothing in my database...
You still haven't imported import com.mysql.jdbc.Statement;
Actually it was a little problem with the above Statement. You helped a lot. Thank you...
@user3543284 I added code i got working the issue was the way you got your name i changed to "+name.getText()+", you done need to escape the " that was your issue also the way you got the name you can just use name.getText.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.