打開資料庫的SQL Server Managerment Studio >資料庫 >打開資料庫會看見"可編程行" >打開有存儲過程 >新建存儲過程 實例: a.(資料庫端) CREATE PROCEDURE [dbo].[get] @hyzk varchar(255), @feibie varcha ...
打開資料庫的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;
}