无法使用XAMPP和Eclipse将值插入数据库

时间:2018-07-16 12:21:52

标签: java mysql jdbc

我是一名学生,是JDBC概念的新手。我有个问题。我使用GUI创建了一个测试Java程序。

它将要求用户在文本字段中输入其名称,并且当用户单击“添加记录”按钮时,应将键入文本字段的文本上载到数据库。我已经创建了一个示例表,它仅包含一个字段。

当我运行上述程序时,异常语句将输出Null错误。但是,自从我 insert data method 包含System.out.print,它可以打印出在文本字段中键入的值。

这是我制作的示例GUI的代码:

    import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

public class UIData extends JFrame {

    private JPanel contentPane;
    public static JTextField textField;
    public static String Name;

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

    /**
     * Create the frame.
     */
    public UIData() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 823, 472);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblInsertName = new JLabel("Insert Name: ");

        textField = new JTextField();
        textField.setColumns(10);

        JButton btnAddRecord = new JButton("Add Record");
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(65)
                    .addComponent(lblInsertName)
                    .addGap(107)
                    .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(149)
                    .addComponent(btnAddRecord))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(12)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addGap(3)
                            .addComponent(lblInsertName))
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(133)
                    .addComponent(btnAddRecord))
        );
        contentPane.setLayout(gl_contentPane);
        btnAddRecord.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                UIDBConnect connect = new UIDBConnect();
                connect.insertData();
            }
        });
    }



}

这是我用于连接到所述数据库的代码

   import java.sql.*;

public class UIDBConnect extends UIData {
 private Connection con;
 private Statement st;
 private ResultSet rs;
 private PreparedStatement pst;
 public static String str = textField.getText();

 public void DBConnect() {
     try {
         Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "");
         st=con.createStatement();

     } catch(Exception ex) {
         System.out.println("Error : " + ex.getMessage());

     }
 }


 public void insertData() {
     try {

         System.out.println("Inserting records into the table...");
         System.out.println(str);
         st = con.createStatement();
         String sql = "INSERT INTO tbltest " +
                       "VALUES ('" + str + "')";
          st.executeUpdate(sql);

     } catch(Exception ex) {
         System.out.println("Error : " + ex.getMessage());

     } 
 }

}

错误打印出like this

我希望我能清楚地解释。

编辑1:使用ex.printStackTrace()时;该错误显示如下:

java.lang.NullPointerException
    at UIDBConnect.insertData(UIDBConnect.java:43)
    at UIData$2.mouseClicked(UIData.java:84)
    at java.awt.AWTEventMulticaster.mouseClicked(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)

0 个答案:

没有答案