标签:
在java中调用带返回值的存储过程的实现
直接上代码:
DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ PROCEDURE `test`.`text_e`(IN param1 INT ,OUT param2 VARCHAR(20)) /*LANGUAGE SQL | [NOT] DETERMINISTIC | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA } | SQL SECURITY { DEFINER | INVOKER } | COMMENT ‘string‘*/ BEGIN SELECT user_name INTO param2 FROM USER WHERE user_id=param1; END$$ DELIMITER ;
java代码:
public static String getUserNameById(int id){ Connection con = BaseDao.getConnection(); CallableStatement proc = null; try { proc = con.prepareCall("{call text_e(?,?)}"); proc.setInt(1, id);//输入 proc.registerOutParameter(2, Types.VARCHAR);//输出 proc.execute(); return proc.getString(2); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { proc.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }
标签:
原文地址:http://www.cnblogs.com/airycode/p/4822450.html