一个类似登陆的存储过程
mysql存储过程:
drop PROCEDURE if EXISTS login;
CREATE PROCEDURE login(IN username int,in password VARCHAR,out count INT)
begin
SELECT count(1) into count from t_user where uname=username and upwd=password;
end;
java程序调用:
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
public
class Test
{
public static void main(String args[]) throws Exception
{
//加载驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获得连接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/数据库名","用户名","密码");
//创建存储过程的对象
CallableStatement c=conn.prepareCall("{call login(?,?)}");
//给存储过程的参数设置值
c.setString(1,"张三");
c.setString(2, "123");
//注册存储过程的第三个参数
c.registerOutParameter(3,java.sql.Types.INTEGER);
c.executeQuery();
int count = Integer.parseInt(c.getString(3));
if(count>0){
System.out.println("登陆成功");
}
conn.close();
}
}