简体   繁体   中英

How to add text from swing text field to mysql table in Java?

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();
    }}

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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM