简体   繁体   中英

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'q'

I'm trying to learn how to work with a database in Java using MySql. I'm having this error:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'q'

which means I'm having type mismatch but I can't figure out why. Here is my code. I have included ResultSetMetaData to show the datatypes of columns.

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


}
}

This is my output

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'

You are setting a String to an numeric column.

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

Probably you need to the following?

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

try this code

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


}
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM