我目前面临着向数据库表写一些关于java的信息的问题。我一直在
java.sql.SQLException:索引:: 1
时缺少IN或OUT参数
请注意,该表已经创建并存在。
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();
}
}
}
答案 0 :(得分: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);
答案 1 :(得分:0)
将您的查询更改为:
String sql = "INSERT INTO T_EQ_STATEMENT_FILE_TRANS_LOG"
+ "(SENTDATE, FILENAME, EMAIL) VALUES "
+ "(sysdate,'" + file.getName() + "','" + email + "')";
你错过了字符串周围的引号,我不确定Oracle的“时间/日期”格式是否可以接受。
评论:更好的做法是像Prabhaker那样使用setParameters!
答案 2 :(得分: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。
答案 3 :(得分: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...");
}