java.sql.SQLException:[Microsoft] [ODBC Excel驱动程序]条件表达式中的数据类型不匹配。错误?

时间:2015-04-03 06:20:17

标签: java excel

执行程序时出现此错误 SQL查询是:从[sheet1 $]中选择标题,价格,数量 选择的记录是: aa,22.0,22 记录总数= 1

java.sql.SQLException: [Microsoft][ODBC Excel Driver] Data type mismatch in criteria expression.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)
    at package1.currentTime.main(currentTime.java:48)

我将插入步骤更改为insert into [Sheet1$] (title, price, qty) values (' "title" ',' "firstName" ',' 999 ')"); 但同样的错误 有什么建议吗?

package package1;
import java.sql.*;

public class currentTime {  // JDK 7 and above
    static Connection con;
    static Statement st;
    ResultSet rs;
   public static void main(String[] args) {
      try (
         // Step 1: Allocate a database "Connection" object
         Connection conn = DriverManager.getConnection(
               "jdbc:odbc:final");  // Access/Excel

         // Step 2: Allocate a "Statement" object in the Connection
         Statement st = conn.createStatement();
      ) {
         // Excel connection, by default, is read-only.
         // Need to turn it off to issue INSERT, UPDATE, ...
         conn.setReadOnly(false);

         // Step 3: Execute a SQL SELECT query, the query result
         //   is returned in a "ResultSet" object.
         // Table name is the sheet's name in the form of [sheet-name$]
         String strSelect = "select title, price, qty from [sheet1$]";
         System.out.println("The SQL query is: " + strSelect); // Echo For debugging

         ResultSet rs = st.executeQuery(strSelect);

         // Step 4: Process the ResultSet by scrolling the cursor forward via next().
         //  For each row, retrieve the contents of the cells with getXxx(columnName).
         System.out.println("The records selected are:");
         int rowCount = 0;
         while(rs.next()) {   // Move the cursor to the next row
            String title = rs.getString("title");
            double price = rs.getDouble("price");
            int    qty   = rs.getInt("qty");
            System.out.println(title + ", " + price + ", " + qty);
            ++rowCount;
         }
         System.out.println("Total number of records = " + rowCount);


         int returnCode = st.executeUpdate(

            "insert into [Sheet1$] (title, price, qty) values ('" + "title" + "','" + "firstName" + "','" + 999 + "')");
         System.out.println(returnCode + " record(s) inserted.");

         // Try UPDATE
         returnCode = st.executeUpdate(
            "update [sheet1$] set qty = qty+1 where id = 1002");
         System.out.println(returnCode + " record(s) updated.");

      } catch(SQLException ex) {
         ex.printStackTrace();
      }
      // Step 5: Close the resources - Done automatically by try-with-resources
   }
}

1 个答案:

答案 0 :(得分:0)

请检查您的查询和该字段的数据类型。从您的查询看起来,您传递的字符串值是预期的整数值。

插入[Sheet1 $](标题,价格,数量)值('“title”','“firstName”','999')“);

根据您的查询,看起来价格不是字符串n但是您将'firstName'作为值传递给它。