java com.mysql.jdbc.MysqlDataTruncation:数据截断:截断的不正确DOUBLE值:“q”

rvpgvaaj  于 2023-04-04  发布在  Java
关注(0)|答案(3)|浏览(134)

我正在尝试学习如何使用MySQL在Java中使用数据库。我遇到了这个错误:
com.mysql.jdbc.MysqlDataTruncation:数据截断:截断的不正确DOUBLE值:“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();

        while(rs.next()){
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
        }
        pstmt.close();
        c.close();

    } catch (Exception e) {         
        System.out.println(e);
    }

}
}

这是我的输出

Total columns: 2
Column Name of 1st column: id
Column Type Name of 1st column: INT
Column Name of 2nd column: name
Column Type Name of 2nd column: VARCHAR
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'q'
uurity8g

uurity8g1#

您正在将String设置为数字列。

PreparedStatement pstmt=c.prepareStatement("update emp2211 set name=? where id=?");
...
pstmt.setInt(1, 800);       
pstmt.setString(2, "q");    // the second ? is referred to id

也许你需要以下几点?

PreparedStatement pstmt=c.prepareStatement("update emp2211 set name=? where id=?");
...
pstmt.setString(1, "q");       
pstmt.setInt(2, 800);
vu8f3i0k

vu8f3i0k2#

试试这个代码

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

        //this is your error
        pstmt.setString(1, "q");
        pstmt.setInt(2, 800);
        pstmt.executeUpdate();

        while(rs.next()){
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
        }
        pstmt.close();
        c.close();

    } catch (Exception e) {         
        System.out.println(e);
    }

}
}
vfwfrxfs

vfwfrxfs3#

只需按照正确顺序输入即可

相关问题