JDBC CallableStatement问题

时间:2016-04-29 08:41:32

标签: java jdbc

请帮我解决问题。我需要打印用户的NAME,但只能得到它的第一个字母。

public static void main(String[] args) {            
    Connection conn = null;
    java.sql.CallableStatement stmt = null;     

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);       

            String sql = "{call retrieveFirstName(?,?)}";
            stmt = conn.prepareCall(sql);

            //Bind IN parameter first, then bind OUT parameter
            int uID = 1;
            ((java.sql.CallableStatement)stmt).setInt(1,uID);
            ((java.sql.CallableStatement)stmt).registerOutParameter(2, java.sql.Types.VARCHAR);

            ((java.sql.CallableStatement)stmt).execute();

              //Retrieve employee name with getXXX method
            String userName = ((java.sql.CallableStatement)stmt).getNString(2);
            System.out.println("User Name with ID:" + uID + " is " + userName);         

        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            //close resources
        }
}

这是一个程序

create procedure retrieveFirstName
@userID int,
@firstName varchar(250) output
as
begin 
select @firstName=first_name
from user_account
where id=@userID
end

1 个答案:

答案 0 :(得分:0)

您将out参数指定为

((java.sql.CallableStatement)stmt).registerOutParameter(2, java.sql.Types.VARCHAR);

没有指定VARCHAR的精度。

请尝试将precision指定为registerOutParameter的第三个参数。