JDBC距喘MySQL5贋刈狛殻
扮寂:2011-04-03 51cto鴬人 leizhimin
匯、桟廠
MySQL5.1
mysql-connector-java-5.1.10
jdk1.5
燕
create table user (
id int(11) not null auto_increment,
name varchar(50) not null,
pswd varchar(50) default null,
pic longblob,
remark longtext,
primary key (id)
);
屈、亟贋刈狛殻
DELIMITER $
DROP PROCEDURE IF EXISTS testprocedure $
CREATE DEFINER=`vcom`@`%` PROCEDURE testprocedure(in in_name varchar (20),in in_pswd varchar(20),out out_id bigint)
BEGIN
insert into user(name,pswd) values(in_name,in_pswd);
select last_insert_id() into out_id;
END $
DELIMITER ;
眉、JDBC距喘贋刈狛殻
import lavasoft.common.DBToolkit;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
/**
* JDBC距喘MySQL5贋刈狛殻
*
* @author leizhimin 2009-12-4 10:33:12
*/
public class ProcedureTest {
public static void main(String[] args) {
testExeProcedure();
}
public static void testExeProcedure() {
Connection conn = DBToolkit.getConnection();
//幹秀距喘贋刈狛殻議圓協吶SQL囂鞘
String sql = "{call testprocedure (?,?,?)}";
try {
//幹秀狛殻峇佩匂
CallableStatement cstmt = conn.prepareCall(sql);
//譜崔秘歌才竃歌
cstmt.setString(1, "wangwu");
cstmt.setString(2, "111111");
cstmt.registerOutParameter (3, Types.BIGINT); //廣過竃歌
cstmt.executeUpdate();
//資函補竃歌方峙?曾嶽圭塀 脅佩?
Long id = cstmt.getLong (3);
//Long id = cstmt.getLong("out_id");
System.out.println("云肝峨 秘方象議id=" + id);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection (conn);
}
}
}
塰佩朔?陣崙岬?
云肝峨秘方象議id=1
Process finished with exit code 0
Java距喘贋刈狛殻載否叟?徽頁蝕窟贋刈狛殻曳熟是佃?厘匆児云貧短亟狛MySQL議贋 刈狛殻?亟貧中議贋刈狛殻歌心阻和中匯鐙鴬猟?
http://www.blogjava.net/sxyx2008/archive/2009/11/24/303497.html
竃侃?http://lavasoft.blog.51cto.com/62575/238613 |