如何在swing中将swing文本字段中的文本添加到mysql表中?

时间:2014-04-17 10:59:58

标签: java mysql swing

我有一个sql表:User,有一个字段:NAME

我创建了一个弹出对话窗口,我应该在其中输入用户名,之后,它应该传递给mysql中的NAME列。所以,这很简单,但我作为初学者遇到了一些错误。有没有办法修复我的代码才能顺利运行?

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

1 个答案:

答案 0 :(得分:2)

单独使用您的代码我会看到一些问题:

static final String MYNAME = JTextField.getText();

也会抛出异常,我发现你不会在任何地方使用它,所以你可以抛弃它。

此外,如果您使用mysql库jar,那么导入错误的

import java.beans.Statement;

拿出来你想要改为使用mysql语句:

import com.mysql.jdbc.Statement;

到目前为止尝试这些更改我将尝试编译您的代码,看看我能得到什么。

EDIT1 ***

在捕获异常时也是一个小小的提示,在你的情况下记录异常总是被认为是好的做法,以下就足够了:

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

EDIT2 ***

这是你的代码的工作版本抱歉这么久:

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