0

我目前在向数据库表写入有关 java 的一些信息时遇到问题。我不断得到

java.sql.SQLException:索引处缺少 IN 或 OUT 参数:: 1

请注意,该表已经创建并存在。

 public static void includeToDB(File file, String email) throws  Exception{
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resSet = null;
    Date now = new Date();

    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        connection = DriverManager.getConnection(CONNECTION_STRING, USERNAME, PASSWORD);
        String sql = "INSERT INTO T_EQ_STATEMENT_FILE_TRANS_LOG"
                + "(SENTDATE, FILENAME, EMAIL) " + "VALUES "
                + "("+ getCurrentTimeStamp() + "," + file.getName() + "," + email + ")";
             /*   "INSERT INTO CAMEL.T_EQ_STATEMENT_FILE_TRANS_LOG" +
                "           VALUES (" +
                            now + "," +
                            file.getName() + "," +
                            email + ");";
                            */
        statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();

        if(resultSet.next()) {
            System.out.println("[DB_Handler] Information about sent file has been written to the DB... ");
        } else {
            System.out.println("[DB_Handler] Something went wrong, and writing the info has failed...");
        }
    } catch (SQLException e) {
        logger.error(EQSFT_ERROR_TYPE.SQL_EXCEPTION.getErrorToPrint(), e);
        logger.error("Messgage: " + e.getMessage());
        logger.error("Error code: " + e.getErrorCode());
        logger.error("SQL state: " + e.getSQLState());
    } catch (Exception e) {
        logger.error(EQSFT_ERROR_TYPE.UNKNOWN_EXCEPTION.getErrorToPrint(), e);
    } finally {
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
4

4 回答 4

1

必须设置 IN 参数时使用PreparedStatement

String sql = "INSERT INTO T_EQ_STATEMENT_FILE_TRANS_LOG(SENTDATE, FILENAME, EMAIL) values (?,?,?)”;
PreparedStatement pstm = connection.preparedStatement(sql);
pstm.setTimeStamp(1,getCurrentTimeStamp());
pstm.setString(2,file.getName());
pstm.setString(3,email);
于 2013-09-12T06:16:04.233 回答
0
public static void includeToDB(File file, String email) throws  Exception{
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resSet = null;
    Date now = new Date();

    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        connection = DriverManager.getConnection(CONNECTION_STRING, USERNAME, PASSWORD);
        String sql = "INSERT INTO T_EQ_STATEMENT_FILE_TRANS_LOG"
                + "(SENTDATE, FILENAME, EMAIL) " + "VALUES "
                + "(?,?,?)";
             /*   "INSERT INTO CAMEL.T_EQ_STATEMENT_FILE_TRANS_LOG" +
                "           VALUES (" +
                            now + "," +
                            file.getName() + "," +
                            email + ");";
                            */
        statement = connection.prepareStatement(sql);
        statement.setTimestamp(1, getCurrentTimeStamp() );
        statement.setString(2, file.getName());
        statement.setString(3,email);

        ResultSet resultSet = statement.executeQuery();

        if(resultSet.next()) {
            System.out.println("[DB_Handler] Information about sent file has been written to the DB... ");
        } else {
            System.out.println("[DB_Handler] Something went wrong, and writing the info has failed...");
        }
    } catch (SQLException e) {
        logger.error(EQSFT_ERROR_TYPE.SQL_EXCEPTION.getErrorToPrint(), e);
        logger.error("Messgage: " + e.getMessage());
        logger.error("Error code: " + e.getErrorCode());
        logger.error("SQL state: " + e.getSQLState());
    } catch (Exception e) {
        logger.error(EQSFT_ERROR_TYPE.UNKNOWN_EXCEPTION.getErrorToPrint(), e);
    } finally {
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

您必须使用如上所示的参数化查询。

您还可以使用 Statement 和 CallableStatement。

http://www.tutorialspoint.com/jdbc/jdbc-statements.htm

于 2013-09-12T06:21:26.210 回答
0

顺便说一句,即使 Javadoc 指定executeQuery永远不会返回null,但executeQuery在您知道不是查询的语句上使用是一种不好的做法。检查是否插入记录的正确方法是使用更新计数:

/* PreparedStatement constructed and used as demonstrated by Prabhaker */
int count = statement.executeUpdate();

if (count > 0) {
  System.out.println("[DB_Handler] Information about sent file has been written to the DB... ");
} else {
  System.out.println("[DB_Handler] Something went wrong, and writing the info has failed...");
}
于 2013-09-12T06:34:20.593 回答
0

将您的查询更改为:

String sql = "INSERT INTO T_EQ_STATEMENT_FILE_TRANS_LOG"
                + "(SENTDATE, FILENAME, EMAIL) VALUES "
                + "(sysdate,'" + file.getName() + "','" + email + "')";

您缺少字符串周围的引号,并且我不确定 Oracle 是否可以接受您的“时间/日期”格式。

评论:更好的做法是按照 Prabhaker 展示的方式使用 setParameters!

于 2013-09-12T06:18:15.980 回答