打开数据库的SQL Server Managerment Studio---->数据库----->打开数据库会看见"可编程行"------->打开有存储过程------->新建存储过程
实例:
a.(数据库端)
CREATE PROCEDURE [dbo].[get]
@hyzk varchar(255),
@feibie varchar(255)
AS
BEGIN
SELECT hyzk,feibie,sysID,updateTime,chuanghao,ksmc FROM BingRenYiLan
WHERE hyzk=@hyzk AND feibie =@feibie
END
b.(java后台调用sql server存储过程):
//测试
public static void main(String[] args) throws SQLException {
Connection con = null;
ResultSet rs = null;
// 调用数据库连接
con = getConnection();
//调用存储过程的语句-------------------
SQLServerCallableStatement call = (SQLServerCallableStatement) con
.prepareCall("{call get(?,?)}");
//设置参数
call.setString(1, "已婚");
call.setString(2, "自费");
//查询结果
rs = call.executeQuery();
//遍历
while (rs.next()) {
System.out.println("婚姻状况(hyzk):" + rs.getString(1));
System.out.println("费别(feibie):"+ rs.getString(2));
System.out.println("主键(sysID):" + rs.getString(3));
System.out.println("更新时间(updateTime):" + rs.getString(4));
System.out.println("床号(chuanghao):"+ rs.getString(5));
System.out.println("科室名称(ksmc):" + rs.getString(6));
// System.out.println();
}
}
// 初始化数据库连接参数
public static Connection getConnection() throws SQLException {
// 初始化变量
Connection con = null;
String url = "jdbc:sqlserver://localhost:1433;databasename=CMCF_qqqqqqqk";
String userName = "sa";
String password = "wang";
// 连接url
try {
// 加载数据库驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
con.close();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}