使用GUI从两个不同的类向MyQSL数据库添加数据

时间:2016-09-30 19:18:49

标签: java mysql user-interface

我有一个简单的作业应用程序GUI,其中有一个类允许用户输入他们的个人信息,另一个类允许用户输入他们的工作历史,我想将所有信息存储在数据库中。我创建了mySQL数据库并成功连接但是当我在其中插入任何内容时,它显示数据库中的值为null。我不知道为什么。到目前为止,这是我的代码:

DataBase Class:

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class ConnectDB 
{
    private String name, address, city, state, email, employer, position, start, end, duties, reason, DOB;
    private int phoneNum, zipCode, employerPhonNum;

    Connection conn = null; 

    /*
    public static void main(String [] args) throws Exception
    {
        ConnectDB db = new ConnectDB();
        db.getConnection();
    }
*/

    public boolean getConnection() throws Exception
    {
        boolean connected = false;

        try
        {
            final String driver = "com.mysql.jdbc.Driver";
            final String url = "jdbc:mysql://localhost:3306/JobApplication"; 
            final String username = "root";
            final String password = "Nasim8055";
            Class.forName(driver); 

            conn = DriverManager.getConnection(url, username, password);
            System.out.println("Connected");
            connected = true;
        }
        catch(Exception e)
        {
            System.out.println(e);
            connected = false;
        }

        return connected;
    }

    /*
    * This method retrieves all the basic information and stores it         
    */
    public void getBasicInfo(String name, String address, String city, String state, int zip, int phoneNum,
            String email, String DOB)
    {
        this.name = name;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipCode = zip;
        this.phoneNum = phoneNum;
        this.email = email;
        this.DOB = DOB;
    }

    /*
    * This method retrieves info from employment history
    */  
    public void getEmploymentInfo(String employer, String position, String start, String end, String duties, String reason, int phoneNum)
    {
        this.employer = employer;
        this.position = position;
        this.start = start;
        this.end = end;
        this.duties = duties;
        this.reason = reason;
        this.employerPhonNum = phoneNum;
    }

    public void insertDB()
    {
        Statement stmt = null;
        int ID = 1;
        try 
        {
            if(getConnection()) // if there is a connection     
            {
                stmt = conn.createStatement();

                String sql = "INSERT INTO applicants " +
                        "VALUES ('"+ID+"', '"+name+"', '"+address+"', '"+city+"', '"+state+"', '"+zipCode+"', '"+phoneNum+"', '"+email+"', '"+employer+"', "
                                + "'"+position+"', '"+start+"', '"+end+"', '"+employerPhonNum+"', '"+duties+"', '"+reason+"')";         
                stmt.executeUpdate(sql);
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmployer() {
        return employer;
    }

    public void setEmployer(String employer) {
        this.employer = employer;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public String getDuties() {
        return duties;
    }

    public void setDuties(String duties) {
        this.duties = duties;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public int getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(int phoneNum) {
        this.phoneNum = phoneNum;
    }

    public int getZipCode() {
        return zipCode;
    }

    public void setZipCode(int zipCode) {
        this.zipCode = zipCode;
    }

    public int getEmployerPhonNum() {
        return employerPhonNum;
    }

    public void setEmployerPhonNum(int employerPhonNum) {
        this.employerPhonNum = employerPhonNum;
    }

    public String getDOB() {
        return DOB;
    }

    public void setDOB(String DOB) {
        this.DOB = DOB;
    }
}

BasicInfo类排除了一些不相关的方法:

public class BasicInfoWindow extends JPanel
{
    private JTextField txtName, txtAddress, txtCity, txtState, txtZipCode, txtPhoneNumber, txtEmail;
    private JComboBox cbDate, cbYear, cbMonth;
    private JLabel labelName, labelAddress, labelCity, labelState, labelZipCode, labelPhoneNumber, labelEmail, labelDOB;
    private JButton btnClear;

    public BasicInfoWindow()
    {
        createView();
    }

    private void createView()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        add(panel);

        northPanel(panel);

        centerPanel(panel);

        southPanel(panel);
    }


    public void writeToDB()
    {
        ConnectDB db = new ConnectDB();
        String DOB = cbMonth.getSelectedItem() + " " + cbDate.getSelectedItem() + " " + cbYear.getSelectedItem();

        db.getBasicInfo(txtName.getText(), txtAddress.getText(), txtCity.getText(), txtState.getText(), 
                Integer.parseInt(txtZipCode.getText()), Integer.parseInt(txtPhoneNumber.getText()), txtEmail.getText(), DOB);
    }

就业历史课程排除不相关的方法

public class EmploymentHistoryWindow extends JPanel
{
    private JButton btnClear, btnNext, btnPrevious, btnAddMoreEmployment;
    private JLabel labelEmployer, labelPosition, labelStart, labelEnd, labelDuties,labelReason, labelPhoneNum;
    private JTextField txtEmployer, txtPosition, txtStart, txtEnd, txtPhoneNum;
    private JTextArea areaDuties, areaReason;

    private JTabbedPane tabbedPane = new JTabbedPane();

    public EmploymentHistoryWindow()
    {
        createView();
    }

    private void createView()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        tabbedPane.add("Employment", panel);

        add(tabbedPane);

        northPanel(panel);

        centerPanel(panel);

        southPanel(panel);
    }

    public void writeToDB()
    {
        ConnectDB db = new ConnectDB();
        db.getEmploymentInfo(txtEmployer.getText(), txtPosition.getText(), txtStart.getText(), txtEnd.getText(), 
                areaDuties.getText(), areaReason.getText(), Integer.parseInt(txtPhoneNum.getText()));
    }

0 个答案:

没有答案