I am working on the insertion a new data to mysql database. 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)...
queryString = "insert into person values ('" + (driverID) + "', '" + (firstName) + "', '" + (lastName) + "', '" + (address)
+ "', '" + (phone) + "')";
(I think you were also missing a close quote on that line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.