我正在尝试学习如何使用 MySql 在 Java 中使用数据库。我遇到这个错误:
com.mysql.jdbc.MysqlDataTruncation:数据截断:截断不正确的双值:'q'
这意味着我的类型不匹配,但我不明白为什么。这是我的代码。我已包含 ResultSetMetaData 来显示列的数据类型。
import java.sql.*;
public class Prep {
public static void main(String[] args) throws SQLException {
try {
Connection c=DriverManager.getConnection(host, username, password);
PreparedStatement pstmt=c.prepareStatement("update emp2211 set name=? where id=?");
ResultSet rs = pstmt.executeQuery("Select * from emp2211");
ResultSetMetaData rsmd= rs.getMetaData();
System.out.println("Total columns: "+rsmd.getColumnCount());
System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));
System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1));
System.out.println("Column Name of 2nd column: "+rsmd.getColumnName(2));
System.out.println("Column Type Name of 2nd column: "+rsmd.getColumnTypeName(2));
pstmt.setInt(1, 800);
pstmt.setString(2, "q");
pstmt.executeUpdate(); …Run Code Online (Sandbox Code Playgroud)