dbutils 调用存储过程 mysql_mysql创建存储过程并通过java程序调用该存储过程

create table users_ning(id primary key auto_increment,pwd int);insert into users_ning values(id,1234);insert into users_ning values(id,12345);insert into users_ning values(id,12);insert into users_nin...
摘要由CSDN通过智能技术生成

create table users_ning(id primary key auto_increment,pwd int);

insert into users_ning values(id,1234);

insert into users_ning values(id,12345);

insert into users_ning values(id,12);

insert into users_ning values(id,123);

CREATE PROCEDURE login_ning(IN p_id int,IN p_pwd int,OUT flag int)

BEGIN

DECLAREv_pwd int;

select pwd INTO v_pwd from users_ning

where id = p_id;

if v_pwd = p_pwd then

set flag:=1;

else

select v_pwd;

set flag := 0;

end if;

END

package demo20130528;

import java.sql.*;

import demo20130526.DBUtils;

/**

* 测试JDBC API调用过程

* @author tarena

*

*/

public class ProcedureDemo2 {

/**

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

System.out.println(login(123, 1234));

}

/**

* 调用过程,实现登录功能

* @param id 考生id

* @param pwd 考试密码

* @return if成功:1; if密码错:0; if没有用户:-1

* @throws Exception

*/

public static int login(int id, int pwd) throws Exception{

int flag = -1;

String sql = "{call login_ning(?,?,?)}";//*****

Connection conn = DBUtils.getConnMySQL();

CallableStatement stmt = null;

try{

stmt = conn.prepareCall(sql);

//传递输入参数

stmt.setInt(1, id);

stmt.setInt(2, pwd);

//注册输出参数,第三个占位符的数据类型是整型

stmt.registerOutParameter(3, Types.INTEGER);//*****

//执行过程

stmt.execute();

//获得过程执行后的输出参数

flag = stmt.getInt(3);//*****

}catch(Exception e){

e.printStackTrace();

}finally{

stmt.close();

DBUtils.dbClose();

}

return flag;

}

}

 
 

package demo20130526;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DatabaseMetaData;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值