建立存储过程,点击数据库既可以建立存储过程,自己根据需求来拓展,下面是一个实例:
1 DELIMITER $$ 2 3 DROP PROCEDURE IF EXISTS `test`.`addUser` $$ 4 5 CREATE PROCEDURE `test`.`addUser` (in name varchar(255), in age int, out id int) 6 7 BEGIN 8 9 insert into user(`name`,`birth_day`,`ver`) values(name,now(), age); 10 11 select last_insert_id() into id; 12 13 END $$ 14 15 DELIMITER ;
下面是一段总结的代码:
1 Connection conn = null; 2 CallableStatement cs = null; 3 ResultSet rs = null; 4 try { 5 // 2.建立连接 6 conn = JdbcUtils.getConnection(); 7 // conn = JdbcUtilsSing.getInstance().getConnection(); 8 // 3.创建语句 9 10 String sql = "{ call addUser(?,?,?,?) } "; 11 cs = conn.prepareCall(sql); 12 cs.registerOutParameter(4, Types.INTEGER); 13 cs.setString(1, "ps name"); 14 cs.setDate(2, new java.sql.Date(System.currentTimeMillis())); 15 cs.setFloat(3, 100f); 16 17 cs.executeUpdate(); 18 19 int id = cs.getInt(4); 20 21 System.out.println("id=" + id);