将Java中的String转换为VARCHAR IN SQL

时间:2015-04-15 17:34:55

标签: java sql database plsql

我正在尝试用Java编写PL / SQL。 我想将一个参数作为String传递,但在SQL过程中它是一个VARCHAR(140)。 但是,当我运行该程序时,它给我一个错误,我不明白为什么?

这是我的SQL程序:

CREATE OR REPLACE PROCEDURE task1
 (input_tweet_id IN integer, input_user_id IN integer, input_time IN timestamp, input_tweet IN varchar(140))
is
begin
    INSERT INTO tweet(tweetid, userid, publishtime, tweet)
    VALUES (input_tweet_id,input_user_id,input_time,input_tweet);
END;

Java调用:

    int in_tweet_id = 0;
    int in_user_id = 0;
    Timestamp in_time =null;
    String in_tweet ="";

 Connection conn = DriverManager.getConnection (connStr, uname, passwd);
 CallableStatement cstmt = conn.prepareCall("{? = call task1(?, ?)}");
       // cstmt.registerOutParameter (1, oracle.jdbc.OracleTypes.CURSOR);
        cstmt.setInt(1, in_tweet_id);
        cstmt.setInt(2, in_user_id);
        cstmt.setTimestamp(3,in_time);
        cstmt.setString(4,in_tweet);

错误:

 at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
        at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:131)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:197)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:261)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:269)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:490)
        at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5013)
        at oracle.jdbc.driver.OracleCallableStatement.setString(OracleCallableStatement.java:4094)
        at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:246)
        at hw4.execute_task1(hw4.java:247)
        at hw4.main(hw4.java:101)

1 个答案:

答案 0 :(得分:5)

你正试图设置第3和第3第4个CallableStatement参数,其中只有2个已定义的

CallableStatement cstmt = conn.prepareCall("{call task1(?, ?, ?, ?)}");