java 怎么调用mysql 存储过程
1个回答
2017-03-22 · 知道合伙人软件行家
关注
展开全部
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;
/*
* 对应的存储过程
DROP PROCEDURE IF EXISTS `pro_num_user`;
delimiter ;;
CREATE PROCEDURE `pro_num_user`(IN user_name varchar(10) ,OUT count_num INT)
READS SQL DATA
BEGIN
SELECT COUNT(*) INTO count_num FROM tab_user WHERE 'name'=user_name;
END
;;
delimiter ;
*/
public class Test {
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://127.0.0.1:3306/test";
public static final String USERNAME = "root";
public static final String PASSWORD = "acbee";
public static void main(String[] args) throws Exception {
test1();
test2();
}
public static void test1() throws Exception
{
Class.forName(DRIVER_CLASS);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "{CALL pro_num_user(?,?)}"; //调用存储过程
CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm
cstm.setString(1, "myd"); //存储过程输入参数
//cstm.setInt(2, 2); // 存储过程输入参数
cstm.registerOutParameter(2, Types.INTEGER); // 设置返回值类型 即返回值
cstm.execute(); // 执行存储过程
System.out.println(cstm.getInt(2));
cstm.close();
connection.close();
}
public static void test2() throws Exception
{
Class.forName(DRIVER_CLASS);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "{CALL pro_number(?,?,?)}"; //调用存储过程
CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm
cstm.setInt(1, 2); // 存储过程输入参数
cstm.setInt(2, 2); // 存储过程输入参数
cstm.registerOutParameter(3, Types.INTEGER); // 设置返回值类型 即返回值
cstm.execute(); // 执行存储过程
System.out.println(cstm.getInt(3));
cstm.close();
connection.close();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;
/*
* 对应的存储过程
DROP PROCEDURE IF EXISTS `pro_num_user`;
delimiter ;;
CREATE PROCEDURE `pro_num_user`(IN user_name varchar(10) ,OUT count_num INT)
READS SQL DATA
BEGIN
SELECT COUNT(*) INTO count_num FROM tab_user WHERE 'name'=user_name;
END
;;
delimiter ;
*/
public class Test {
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://127.0.0.1:3306/test";
public static final String USERNAME = "root";
public static final String PASSWORD = "acbee";
public static void main(String[] args) throws Exception {
test1();
test2();
}
public static void test1() throws Exception
{
Class.forName(DRIVER_CLASS);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "{CALL pro_num_user(?,?)}"; //调用存储过程
CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm
cstm.setString(1, "myd"); //存储过程输入参数
//cstm.setInt(2, 2); // 存储过程输入参数
cstm.registerOutParameter(2, Types.INTEGER); // 设置返回值类型 即返回值
cstm.execute(); // 执行存储过程
System.out.println(cstm.getInt(2));
cstm.close();
connection.close();
}
public static void test2() throws Exception
{
Class.forName(DRIVER_CLASS);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "{CALL pro_number(?,?,?)}"; //调用存储过程
CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm
cstm.setInt(1, 2); // 存储过程输入参数
cstm.setInt(2, 2); // 存储过程输入参数
cstm.registerOutParameter(3, Types.INTEGER); // 设置返回值类型 即返回值
cstm.execute(); // 执行存储过程
System.out.println(cstm.getInt(3));
cstm.close();
connection.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询