2015-04-15 4 views
-1

Я работаю над вставкой новых данных в базу данных mysql. Я получил сообщение об ошибке, чтоВставить новую запись в таблицу в GUI java

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 

Может кто-нибудь показать мне, что не так с моим кодом? Вот мой код:

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

Вот мои таблицы. У меня есть две таблицы

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

Благодарим за помощь!

+2

I» d рекомендуем a) распечатать те объекты String, которые вы используете в качестве запросов, которые сделают любые ошибки в синтаксисе более читабельными; b) использовать PreparedStatement для выполнения запросов. – copeg

ответ

2

Вы отсутствуют запятые в операторах INSERT (вы использовали плюс знаки вместо) ...

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

(я думаю, что вы также не хватает близкого цитаты по этой линии)

+0

о, ничего себе. Спасибо огромное! это сработало – John

Смежные вопросы