Nin7a
Nin7a
采纳率0%
2017-11-19 02:40

java连接mysql数据库,用的图形界面实现注册功能,不过报了很多错误,大神能帮忙看看吗

 package Package1;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;


public class Register  extends JFrame{
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_2;
    private JPasswordField passwordField;
    private JTextField textField_1;
    private JTextField textField_3;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Register frame = new Register();
                    frame.setVisible(true);

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

    /**
     * Create the frame.
     */
    public Register() throws Exception{
        super("注册界面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 526, 432);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        textField = new JTextField();
        textField.setBounds(151, 33, 223, 24);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label = new JLabel("用户名:");
        label.setBounds(32, 36, 72, 18);
        contentPane.add(label);

        JLabel label_1 = new JLabel("\u5BC6  \u7801\uFF1A");
        label_1.setBounds(32, 88, 72, 18);
        contentPane.add(label_1);

        JLabel label_2 = new JLabel("\u59D3  \u540D\uFF1A");
        label_2.setBounds(32, 138, 72, 18);
        contentPane.add(label_2);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(151, 135, 223, 24);
        contentPane.add(textField_2);

        passwordField = new JPasswordField();
        passwordField.setBounds(151, 85, 223, 24);
        contentPane.add(passwordField);

        JLabel label_3 = new JLabel("\u5B66  \u53F7\uFF1A");
        label_3.setBounds(32, 187, 72, 18);
        contentPane.add(label_3);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(151, 184, 223, 24);
        contentPane.add(textField_1);

        JLabel label_4 = new JLabel("\u8054\u7CFB\u65B9\u5F0F\uFF1A");
        label_4.setBounds(32, 239, 83, 18);
        contentPane.add(label_4);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(151, 236, 223, 24);
        contentPane.add(textField_3);
        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setBounds(32, 300, 113, 24);
        contentPane.add(textArea);

        JButton button = new JButton("\u5B8C\u6210\u6CE8\u518C");
        button.setBounds(185, 298, 113, 27);
        contentPane.add(button);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String passwords;//密码
                String usernames;//用户名
                String names;//姓名
                String ids;//学号
                String phones;//联系方式
                char[] pswd=passwordField.getPassword();
                passwords=String.valueOf(pswd);
                usernames=textField.getText();
                names=textField_2.getText();
                ids=textField_1.getText();
                phones=textField_3.getText();

                try {
                    Connection1 contosql = null;
                    contosql = new Connection1();//连接数据库
                    if(passwords.equals("")||usernames.equals("")||names.equals("")||ids.equals("")||phones.equals("")) {//五个选项任意一个为空的话
                        textArea.setText("请填写完整!");
                    }
                    else {
                        PreparedStatement psql=contosql.con.prepareStatement("insert into userinfo(username,password,name,id,phone)"+"value(?,?,?,?,?)");
                        psql.setString(1, usernames);
                        psql.setString(2, passwords);
                        psql.setString(3, names);
                        psql.setString(4,ids);
                        psql.setString(5, phones);
                        psql.executeUpdate();
                        textArea.setText("注册成功!");
                        psql.close();
                    }
                    contosql.con.close();

                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });
    }
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • Nin7a Nin7a 4年前

    谢谢各位我已经找出问题了

    点赞 评论 复制链接分享
  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 4年前

    代码是你写的还是抄的
    如果是抄的,那么请你检查数据库的配置,以及数据库表的定义和程序是否匹配。
    如果是自己写的,建议你找一个现成的程序研究下。

    点赞 评论 复制链接分享
  • Nin7a Nin7a 4年前

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Package1.Register$2.actionPerformed(Register.java:138)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    这是报的错。。138行是contosql.con.close();

    点赞 评论 复制链接分享

为你推荐