Can anybody point out what's wrong with my code? I am trying to update an attribute but I got an error
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'phone' at row 1
Is there anything wrong with the SQL command? Thank you!
String queryString = "";
String selectedItem = (String) searchTypeCmb.getSelectedItem();
String searchTerm = searchTermField.getText();
String id = theId.getText();
textArea.setText("");
if ("Phone".equals(selectedItem)) {
queryString = "UPDATE person SET phone = '" + (searchTerm) +
" WHERE driverID = " + (id) + "'";
}
else if ("Address".equals(selectedItem)) {
queryString = "UPDATE person SET address = '" + (searchTerm) +
" WHERE driverID = " + (id) + "'";
}
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/dealer", "root", "admin");
statement = connection.createStatement();
statement.executeUpdate(queryString);
Here is my DB schema
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 char(20)
);
create table cars
( license char(10) not null,
brand char(20) not null,
model char(20) not null,
year char(10),
status char(10) not null,
carID int unsigned not null primary key
);
Phonecolumn?'), in both cases. Also please consider using prepared statements.varcharand increase the size to what you want to have.