2016-11-30 275 views
-1

我想了解如何使用MySql在Java中使用數據庫。我有這個錯誤:com.mysql.jdbc.MysqlDataTruncation:數據截斷:截斷不正確DOUBLE值:'q'

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: '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' 
+0

你應該*不*重用'PreparedStatement'作爲常規'Statement'。爲'executeQuery(「...」)'調用創建一個單獨的Statement對象。 – Andreas

+0

'Statement s = c.createStatement(); \t ResultSet rs = s.executeQuery(「Select * from emp2211」);' 是這樣的嗎?每次我需要調用'executeQuery'時,我必須創建'Statement'對象嗎? – hrittwik

+0

不,但調用'executeQuery'會在同一個['Statement'](https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html)關閉任何以前的ResultSet''。目的。來自javadoc:*默認情況下,每個Statement對象只能同時打開一個ResultSet對象。 [...] **如果存在一個打開的ResultSet對象,那麼Statement接口中的所有執行方法隱式關閉語句的當前ResultSet對象**。*因此,我驚訝地發現'PreparedStatement.executeUpdate()'沒有這樣做。可能依賴於驅動程序。 – Andreas

回答

2

您設置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);  
1

試試這個代碼

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


} 
} 
+0

說明會澄清答案。 – Andreas

+1

parameterIndex - 第一個參數是1,第二個參數是2,就像這樣:PreparedStatement pstmt = con.prepareStatement(「UPDATE EMPLOYEES SET SALARY =?WHERE ID =?」); pstmt.setBigDecimal(1,153833.00) pstmt.setInt(2,110592) –

相關問題