读书人

连载3:模拟JavaEye论坛首页存储过程

发布时间: 2012-12-24 10:43:13 作者: rapoo

连载三:模拟JavaEye论坛首页存储过程


连载3:模拟JavaEye论坛首页存储过程


连载3:模拟JavaEye论坛首页存储过程

?

?

我使用的PLsql 由于没有使用LOOP ?CURSOR ?所以 ?java 代码会比较多 ?以后在发布 一个新版本

?

?

DELIMITER $$DROP PROCEDURE IF EXISTS `anywhere`.`setp2`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `setp2`(IN v_category_id INT,OUT v_category_name VARCHAR(20),OUT v_latest_theme_aboutoneCategory VARCHAR(20),OUT v_theme_num_aboutoneCategory INT)    BEGIN       select name into  v_category_name from category where id=v_category_id;       select title into v_latest_theme_aboutoneCategory from theme where theme.id = (select max(id) from theme where theme.category_id=v_category_id);       select count(*) into v_theme_num_aboutoneCategory from theme where theme.category_id=v_category_id  ;     END$$DELIMITER ;
?

?

public static void test2() throws SQLException{    Connection con = JDBCUtil.getConnection();        PreparedStatement ps = con.prepareStatement("select id from category ");    ResultSet rs = ps.executeQuery();    CallableStatement call =null;    while(rs.next()){    int id = rs.getInt(1);    call = con.prepareCall("{call setp2(?,?,?,?)}");    call.setInt(1, id);    call.registerOutParameter(2,Types.VARCHAR);    call.registerOutParameter(3, Types.VARCHAR);    call.registerOutParameter(4, Types.INTEGER);        call.execute();        System.out.println(call.getString(3));            }call.close();con.close();}
1 楼 程序新手 2011-02-22 哥们你很有勇气,前两个你的帖子可能被投了新手帖,但你还能坚持,佩服佩服,看得出来兴趣能战胜一切,加油~ 2 楼 hyee 2011-02-22 哥们,你那个存储过程是基于哪个数据库的,据我所知Oracle的PLSQL的语法和你贴的差别不小。

另外,如果你的category有10条记录,那么你那段JAVA总共要调用11次数据库,事实上1条SQL就可以搞定了。如果需要保证效率,category也需要有良好的设计。

读书人网 >编程

热点推荐