将数据插入mySQL失败:SQLException java.lang.NullPointerException

时间:2017-06-01 15:30:22

标签: java mysql swing

我有2个表:employee和job_history。包含ID的属性的数据类型是CHAR(5)。当我测试调用ResultSet getEmID = con.excuteQuery("SELECT MAX(RIGHT(Employee_ID,4)) FROM employee");上的聚合函数时,它可以工作。但是现在当我尝试输入完整的数据时,它会出错。有人可以解释一下吗?

这是我的代码:`

package mainProgram;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import javax.swing.*;

public class newEmployee extends JFrame implements ActionListener,KeyListener{
    String check = "";
    Connect con = new Connect();
    JPanel mainPanel,bottomPanel,panel;
    JLabel lblName,lblEmail,lblPhone,lblSallary,lblJob,lblDepartment;
    JTextField txtName,txtEmail,txtPhone,txtSallary,txtJob,txtDepartment;
    JButton insBtn,rstBtn;
    void init(){
        mainPanel = new JPanel(new GridLayout(6,2));
        bottomPanel = new JPanel(new GridLayout(1,2));
        panel = new JPanel(new GridLayout(2,1));
        lblName = new JLabel("Name");
        lblEmail = new JLabel("Email");
        lblPhone = new JLabel("Phone");
        lblSallary = new JLabel("Sallary");
        lblJob = new JLabel("Job");
        lblDepartment = new JLabel("Department");
        txtName = new JTextField();
        txtEmail = new JTextField();
        txtPhone = new JTextField();
        txtSallary = new JTextField();
        txtJob = new JTextField();
        txtDepartment = new JTextField();
        insBtn = new JButton("Next");
        rstBtn = new JButton("Reset");

        mainPanel.add(lblName);
        mainPanel.add(txtName);
        mainPanel.add(lblEmail);
        mainPanel.add(txtEmail);
        mainPanel.add(lblPhone);
        mainPanel.add(txtPhone);
        mainPanel.add(lblSallary);
        mainPanel.add(txtSallary);
        mainPanel.add(lblDepartment);
        mainPanel.add(txtDepartment);
        mainPanel.add(lblJob);
        mainPanel.add(txtJob);

        bottomPanel.add(insBtn);
        bottomPanel.add(rstBtn);

        panel.add(mainPanel);
        panel.add(bottomPanel);

        add(panel);
    }
    public newEmployee() {
        setSize(650,450);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        init();
        setVisible(true);
        insBtn.addActionListener(this);
        rstBtn.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==insBtn){
            String departmentName = txtDepartment.getText();
            String jobName = txtJob.getText();
            ResultSet getEmID = con.excuteQuery("SELECT MAX(RIGHT(Employee_ID,4)) FROM employee");
            ResultSet getDepID = con.excuteQuery("SELECT Department_ID FROM departments WHERE Department_Name = '"+departmentName+"'");
            ResultSet getJobID = con.excuteQuery("SELECT Job_ID FROM jobs WHERE Job_Name = '"+jobName+"'");
            ResultSet getManID = con.excuteQuery("SELECT Manager_ID FROM departments WHERE Department_Name = '"+departmentName+"'");
            //convert hasil query ke string
            String employeeId = "";
            String departmentId = "";
            String jobId = "";
            String managerId = "";
            try {
                employeeId = getEmID.getString("MAX(RIGHT(Employee_ID,4))");
                departmentId = getDepID.getString("Department_ID"); 
                jobId = getJobID.getString("Job_ID");
                managerId = getManID.getString("Manager_ID");

            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            Integer employeeID = Integer.parseInt(employeeId)+1;

            String newID = "E"+employeeID;
            String name = txtName.getText();
            String email = txtEmail.getText();
            String phone = txtPhone.getText();
            String sallary = txtSallary.getText();

            //get current date
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            LocalDate locale = LocalDate.now();
            String date = dtf.format(locale);

            con.executeUpdate("INSERT INTO employee(Employee_ID,Employee_Name,Email,Phone_Number,Job_ID,Hire_Date,Sallary,Manager_ID,Department_ID)VALUES('"+newID+"','"+name+"','"+email+"','"+phone+"','"+jobId+"','"+date+"','"+sallary+"','"+managerId+"','"+departmentId+"')");
            con.executeUpdate("INSERT INTO job_history(Employee_ID,Job_ID,Department_ID)VALUES('"+newID+"','"+jobId+"','"+departmentId+"')");
            JOptionPane.showMessageDialog(this, "Insert Success!");
            dispose();
            new newEmployeeDetail(newID);
        }

    }
    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void keyTyped(KeyEvent e) {
        if(e.getSource()==txtEmail){
            check = txtEmail.getText();
            if(check.contains("@")&&check.contains(".com")){
                JOptionPane.showMessageDialog(this, "Invalid email name!");
            }
        }
        if(e.getSource()==txtJob){
            check = txtJob.getText();
            if(!check.equals("Product Technician")&&!check.equals("System Analyst")&&!check.equals("Programmer")&&!check.equals("Junior Manager")){
                JOptionPane.showMessageDialog(this, "Job must \"Product Technician\" or \"System Analyst\" or \"Programmer\" or \"Junior Manager\"");
            }
        }

    }

}

` 这是我得到的错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mysql.jdbc.ResultSet.buildIndexMapping(ResultSet.java:607)
at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:963)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:5584)
at mainProgram.newEmployee.actionPerformed(newEmployee.java:86)
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)

0 个答案:

没有答案