简体   繁体   English

在GUI Java中将新记录插入表中

[英]Insert a new record to table in GUI java

I am working on the insertion a new data to mysql database. 我正在向MySQL数据库插入新数据。 I got an error says that 我有一个错误说

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '')' at
 line 1

Can anybody show me what is wrong with my code? 有人可以告诉我我的代码有什么问题吗? Here is my code: 这是我的代码:

public class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        String queryString = "";
        String queryString2 = "";
        String outputString = "";
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/dealer", "root", "admin");
            statement = connection.createStatement();
            String driverID = driverIdTextField.getText();
            String firstName = firstNameTextField.getText();
            String lastName = lastNameTextField.getText();
            String address = addressTextField.getText();
            String phone = phoneTextField.getText();
            String license = licenseTextField.getText();
            String brand = brandTextField.getText();
            String model = modelTextField.getText();
            String year = yearTextField.getText();
            String selectItem = (String) carStatus.getSelectedItem();

            queryString = "insert into person values ('" + (driverID) + "' + '" + (firstName) + "' +  '" + (lastName) + "' + '" + (address) 
                + "' + '" + (phone) + ")";
            queryString2 = "insert into cars values ('" + (license) + "' + '" + (brand) + "' + '" + (model) + "' + '" + (year)
                + "' + '" + (selectItem) + ")";

            statement.executeUpdate(queryString);
            statement.executeUpdate(queryString2);
            connection.close();
        } catch (SQLException sqlException){
                sqlException.printStackTrace();
        } catch ( ClassNotFoundException x ) {
            x.printStackTrace();
        } catch ( InstantiationException x ) {
            x.printStackTrace();
        } catch ( IllegalAccessException x ) {
            x.printStackTrace();
        }
    }

Here is my tables. 这是我的桌子。 I have two tables 我有两张桌子

create table person
( driverID int unsigned not null primary key,
  firstName char(20) not null,
  lastName char(20) not null,
  address char(30) not null,
  phone int unsigned
);

create table cars
( license char(10) not null primary key,
  brand char(20) not null,
  model char(20) not null,
  year date,
  status char(10) not null
);

Thank you for your help! 谢谢您的帮助!

You are missing commas in your INSERT statements (you've used plus signs instead)... 您在INSERT语句中缺少逗号(改为使用加号)...

queryString = "insert into person values ('" + (driverID) + "', '" + (firstName) + "',  '" + (lastName) + "', '" + (address) 
            + "', '" + (phone) + "')";

(I think you were also missing a close quote on that line) (我认为您在该行上也缺少一个引号)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM