Microsoft sql错误异常

时间:2015-11-20 04:06:25

标签: java sql-server-2008 exception-handling

 try {
        //     con = dbConn.mySqlConnection();
            con = DatabaseConnection.getRAWConnection();
 String insertSqlTable = "insert into certification VALUES(?,?,?,?,?,?,CURRENT_TIMESTAMP,?)";
          pst = con.prepareStatement(insertSqlTable);
          pst.setInt(1, td.getEmpId());
        pst.setString(2, td.getRname());
        pst.setString(3, td.getStream());
        pst.setString(4, td.getCertificationType());
        pst.setString(5, td.getCertificationName());
        pst.setString(6, td.getCertificationDate());
        //pst.setTimestamp(7, timestamp);
        pst.setInt(7, td.getScore());

        int count = pst.executeUpdate();
        if (count >= 1) {
            con.commit();
            status = true;
        } else {
            System.out.println("Error occured while inserting certification details into database");
            con.rollback();
            status = false;
        }

我遇到以下异常:

  

com.microsoft.sqlserver.jdbc.SQLServerException:操作数类型冲突:   int与datetime2不兼容

2 个答案:

答案 0 :(得分:2)

而不是这个

pst.setInt(7, td.getScore());

使用

pst.setInt(8, td.getScore());

7th参数已作为CURRENT_TIMESTAMP存在。您有兴趣设置8th一个。

<强>更新

试试这个

try {
        //  con = dbConn.mySqlConnection();
            con = DatabaseConnection.getRAWConnection();
 String insertSqlTable = "insert into certification VALUES(?,?,?,?,?,?,?,?)";
        pst = con.prepareStatement(insertSqlTable);
        pst.setInt(1, td.getEmpId());
        pst.setString(2, td.getRname());
        pst.setString(3, td.getStream());
        pst.setString(4, td.getCertificationType());
        pst.setString(5, td.getCertificationName());
        pst.setString(6, td.getCertificationDate());
        java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
        pst.setTimestamp(7, date);
        pst.setInt(8, td.getScore());

        int count = pst.executeUpdate();
        if (count >= 1) {
            con.commit();
            status = true;
        } else {
            System.out.println("Error occured while inserting certification details into database");
            con.rollback();
            status = false;
        }

}

答案 1 :(得分:0)

可能是日期时间类型的变量得分,这就是它给出此异常的原因,只是尝试使用Calender类的setTime()而不是setInt()。