//静态语句块加载
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
spImoocSelect();
spSelectFilter();
spSelectCount();
}
/**
* 调用无参的存储过程
*/
public static void spImoocSelect(){
try {
CallableStatement cs = conn.prepareCall("call sp_imooc_select()");
cs.execute();
ResultSet rs = cs.getResultSet();
while(rs.next()){
System.out.println(rs.getString("user_name") + rs.getString("mobile"));
}
} catch (SQLException e) {
System.out.println("查询失败! ");
}
}
/**
* 调用输入参数的存储过程
*/
public static void spSelectFilter(){
try {
System.out.println("请输入参数! ");
Scanner s = new Scanner(System.in);
String in = s.next();
CallableStatement cs = conn.prepareCall("call sp_select_filter(?)");
cs.setString(1, in);
cs.execute();
ResultSet rs = cs.getResultSet();
while(rs.next()){
System.out.println(rs.getString("user_name") + rs.getString("mobile"));
}
} catch (SQLException e) {
System.out.println("查询失败! ");
}
}
/**
* 调用输出参数的存储过程
*/
public static void spSelectCount(){
try {
CallableStatement cs = conn.prepareCall("call sp_select_count(?)");
//定义输出参数类型
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
//得到输出参数的结果
int count = cs.getInt(1);
System.out.println(count);
} catch (SQLException e) {
e.printStackTrace();
}
}
点击查看更多内容
6人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦