//静态语句块加载
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();
}
}
打开App,阅读手记
热门评论
建立连接的代码如果放在静态代码块中只能获得一次连接,如果多次操作要放在外面,到后面与html结合的时候,一刷新页面就无法再次连接了
来过来?不是看过来么?