Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用GUI从两个不同的类向MySQL数据库添加数据_Java_Mysql_User Interface - Fatal编程技术网

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

Java 使用GUI从两个不同的类向MySQL数据库添加数据,java,mysql,user-interface,Java,Mysql,User Interface,我有一个简单的工作应用程序GUI,其中一个类允许用户输入他们的个人信息,另一个类允许用户输入他们的工作历史,我希望将所有信息存储在数据库中。我创建了mySQL数据库并成功连接,但当我在其中插入任何内容时,它显示数据库中的值为空。我不知道为什么。以下是我目前的代码: 数据库类: package database; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; publi

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

数据库类:

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

是否存在与异常相关的异常,或者只是看不到表中的值?表中的值显示为null,没有异常可能是您试图执行的查询,可能是格式错误。你能打印出来看看吗?请看,因为你的查询看起来像是完全有缺陷的旧PHP代码,包括sql注入。它打印出所有空值。所以我猜测,当我将数据从类传递到数据库类时,它没有正确地传递。我不知道如何解决这个问题。是否存在与异常相关的问题,或者只是看不到表中的值?表中的值显示为null,没有异常可能是您试图执行的查询,可能是格式错误。你能打印出来看看吗?请看,因为你的查询看起来像是完全有缺陷的旧PHP代码,包括sql注入。它打印出所有空值。所以我猜测,当我将数据从类传递到数据库类时,它没有正确地传递。我不知道如何解决这个问题。