用Java更新mysql数据库
有人能指出我的代码有什么问题吗?我正在尝试更新属性,但出现错误 com.mysql.jdbc.MysqlDataTruncation:数据截断:数据太长,无法访问 第1行的“电话”列 SQL命令有什么问题吗?谢谢大家!用Java更新mysql数据库,java,mysql,Java,Mysql,有人能指出我的代码有什么问题吗?我正在尝试更新属性,但出现错误 com.mysql.jdbc.MysqlDataTruncation:数据截断:数据太长,无法访问 第1行的“电话”列 SQL命令有什么问题吗?谢谢大家! String queryString = ""; String selectedItem = (String) searchTypeCmb.getSelectedItem(); String searchTerm = searchTermField.getText(); Stri
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);
这是我的数据库模式
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
);
首先,文本周围缺少的结束引号
"UPDATE person SET phone = '" + (searchTerm) + " WHERE driverID = " + (id) + "'"
^-----------------------------^
你可能想成为这样的人
"UPDATE person SET phone = '" + (searchTerm) + "' WHERE driverID = " + (id)
尽管如此,我还是强烈建议您使用PreparedStatements
String queryString = "";
String selectedItem = (String) searchTypeCmb.getSelectedItem();
String searchTerm = searchTermField.getText();
String id = theId.getText();
textArea.setText("");
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/dealer", "root", "admin")) {
PreparedStatement stmt = null;
if ("Phone".equals(selectedItem)) {
stmt = connection.prepareStatement("UPDATE person SET phone = ? WHERE driverID = ?");
} else if ("Address".equals(selectedItem)) {
stmt = connection.prepareStatement("UPDATE person SET address = ? WHERE driverID = ?");
}
if (stmt != null) {
stmt.setString(1, searchTerm);
stmt.setString(2, id);
stmt.executeUpdate();
}
有关更多详细信息,请参见…首先,您的文字周围缺少结束语引号
"UPDATE person SET phone = '" + (searchTerm) + " WHERE driverID = " + (id) + "'"
^-----------------------------^
你可能想成为这样的人
"UPDATE person SET phone = '" + (searchTerm) + "' WHERE driverID = " + (id)
尽管如此,我还是强烈建议您使用PreparedStatements
String queryString = "";
String selectedItem = (String) searchTypeCmb.getSelectedItem();
String searchTerm = searchTermField.getText();
String id = theId.getText();
textArea.setText("");
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/dealer", "root", "admin")) {
PreparedStatement stmt = null;
if ("Phone".equals(selectedItem)) {
stmt = connection.prepareStatement("UPDATE person SET phone = ? WHERE driverID = ?");
} else if ("Address".equals(selectedItem)) {
stmt = connection.prepareStatement("UPDATE person SET address = ? WHERE driverID = ?");
}
if (stmt != null) {
stmt.setString(1, searchTerm);
stmt.setString(2, id);
stmt.executeUpdate();
}
有关更多详细信息,请参见…首先,您的文字周围缺少结束语引号
"UPDATE person SET phone = '" + (searchTerm) + " WHERE driverID = " + (id) + "'"
^-----------------------------^
你可能想成为这样的人
"UPDATE person SET phone = '" + (searchTerm) + "' WHERE driverID = " + (id)
尽管如此,我还是强烈建议您使用PreparedStatements
String queryString = "";
String selectedItem = (String) searchTypeCmb.getSelectedItem();
String searchTerm = searchTermField.getText();
String id = theId.getText();
textArea.setText("");
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/dealer", "root", "admin")) {
PreparedStatement stmt = null;
if ("Phone".equals(selectedItem)) {
stmt = connection.prepareStatement("UPDATE person SET phone = ? WHERE driverID = ?");
} else if ("Address".equals(selectedItem)) {
stmt = connection.prepareStatement("UPDATE person SET address = ? WHERE driverID = ?");
}
if (stmt != null) {
stmt.setString(1, searchTerm);
stmt.setString(2, id);
stmt.executeUpdate();
}
有关更多详细信息,请参见…首先,您的文字周围缺少结束语引号
"UPDATE person SET phone = '" + (searchTerm) + " WHERE driverID = " + (id) + "'"
^-----------------------------^
你可能想成为这样的人
"UPDATE person SET phone = '" + (searchTerm) + "' WHERE driverID = " + (id)
尽管如此,我还是强烈建议您使用PreparedStatements
String queryString = "";
String selectedItem = (String) searchTypeCmb.getSelectedItem();
String searchTerm = searchTermField.getText();
String id = theId.getText();
textArea.setText("");
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/dealer", "root", "admin")) {
PreparedStatement stmt = null;
if ("Phone".equals(selectedItem)) {
stmt = connection.prepareStatement("UPDATE person SET phone = ? WHERE driverID = ?");
} else if ("Address".equals(selectedItem)) {
stmt = connection.prepareStatement("UPDATE person SET address = ? WHERE driverID = ?");
}
if (stmt != null) {
stmt.setString(1, searchTerm);
stmt.setString(2, id);
stmt.executeUpdate();
}
有关更多详细信息,请参见…,如果您有此消息
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'phone'
您在“phone”列中写入的数据长度超过数据库中该字段的最大长度。
因此,您可以:
- 在将电话值插入数据库之前截断电话值
- 更改数据库中的列长度
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'phone'
您在“phone”列中写入的数据长度超过数据库中该字段的最大长度。
因此,您可以:
- 在将电话值插入数据库之前截断电话值
- 更改数据库中的列长度
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'phone'
您在“phone”列中写入的数据长度超过数据库中该字段的最大长度。
因此,您可以:
- 在将电话值插入数据库之前截断电话值
- 更改数据库中的列长度
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'phone'
您在“phone”列中写入的数据长度超过数据库中该字段的最大长度。
因此,您可以:
- 在将电话值插入数据库之前截断电话值
- 更改数据库中的列长度
手机列的数据类型是什么?请检查冒号的大小。我认为这比您要插入的数据要少。我们需要查看DB模式:它告诉您列类型可能太小。在这两种情况下,您还缺少括起来的引号(”
)。也请考虑使用准备好的语句。只需将其设置为<代码> VARCHAR 并将大小增加到您想要的。什么样的数据类型是您的代码>电话< /代码>列?请检查COROM的大小。我认为这比您要插入的数据要少。我们需要查看DB模式:它告诉您列类型可能太小。在这两种情况下,您还缺少括起来的引号(”
)。也请考虑使用准备好的语句。只需将其设置为<代码> VARCHAR 并将大小增加到您想要的。什么样的数据类型是您的代码>电话< /代码>列?请检查COROM的大小。我认为这比您要插入的数据要少。我们需要查看DB模式:它告诉您列类型可能太小。在这两种情况下,您还缺少括起来的引号(”
)。也请考虑使用准备好的语句。只需将其设置为<代码> VARCHAR 并将大小增加到您想要的。什么样的数据类型是您的代码>电话< /代码>列?请检查COROM的大小。我认为这比您要插入的数据要少。我们需要查看DB模式:它告诉您列类型可能太小。在这两种情况下,您还缺少括起来的引号(”
)。也请考虑使用准备好的语句。只需将其设置为<代码> VARCHAR 并将大小增加到您想要的大小。这就使得一个漂亮的转换代码“MadProgrammer说,<代码> PraveRealsStays/Cuth>是对此类查询的最佳实践,这使得一个非常好的变化。”MadProgrammer说。PreparedStatements
是这类查询的最佳实践,它可以做出很好的更改,如@MadProgrammer所说,PreparedStatements
是这类查询的最佳实践,如@MadProgrammer所说,PreparedStatements
是这类查询的最佳实践