jsp执行mysql带参数的存储过程
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="123";
//数据库名
String dbName="stock";
//表名
String tableName="users";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url);
String WhereCondition="age<=30";
String OrderByExpress="id Desc";
//调用存储过程的方法,并且传递了参数,不传参数就 call GetUsersDynamic可以了
CallableStatement cstmt=conn.prepareCall("{call GetUsersDynamic(?,?)}");
cstmt.setString(1,WhereCondition);
cstmt.setString(2,OrderByExpress);
ResultSet rs = cstmt.executeQuery();
//获得数据结果集合
ResultSetMetaData rsmd = rs.getMetaData();
//确定数据集的列数,亦字段数
int numColumns=rsmd.getColumnCount();
// 输出每一个列名
out.print("<table border=1><tr>");
for(int i=1;i<=numColumns;i++)//注意下标从1开始的
{
out.print("<td>"+rsmd.getColumnName(i)+"</td>");
}
out.print("</tr>");
while(rs.next())
{
out.print("<tr>");
for(int i=1;i<=numColumns;i++)
{
//输出数据
out.print("<td>"+rs.getString(i)+"</td>");
}
out.print("</tr>");
}
out.print("</table>");
out.print("数据库操作成功,恭喜你");
rs.close();
cstmt.close();
conn.close();
%>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="123";
//数据库名
String dbName="stock";
//表名
String tableName="users";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url);
String WhereCondition="age<=30";
String OrderByExpress="id Desc";
//调用存储过程的方法,并且传递了参数,不传参数就 call GetUsersDynamic可以了
CallableStatement cstmt=conn.prepareCall("{call GetUsersDynamic(?,?)}");
cstmt.setString(1,WhereCondition);
cstmt.setString(2,OrderByExpress);
ResultSet rs = cstmt.executeQuery();
//获得数据结果集合
ResultSetMetaData rsmd = rs.getMetaData();
//确定数据集的列数,亦字段数
int numColumns=rsmd.getColumnCount();
// 输出每一个列名
out.print("<table border=1><tr>");
for(int i=1;i<=numColumns;i++)//注意下标从1开始的
{
out.print("<td>"+rsmd.getColumnName(i)+"</td>");
}
out.print("</tr>");
while(rs.next())
{
out.print("<tr>");
for(int i=1;i<=numColumns;i++)
{
//输出数据
out.print("<td>"+rs.getString(i)+"</td>");
}
out.print("</tr>");
}
out.print("</table>");
out.print("数据库操作成功,恭喜你");
rs.close();
cstmt.close();
conn.close();
%>