1>带参数的新增用户存储过程:
CREATE PROCEDURE [dbo].[p_Insert_User]
@name nvarchar(50),
@UserPwd nvarchar(50)
AS
BEGIN
INSERT INTO tb_User VALUES(NEWID(),@name,@UserPwd)
END
2>不带参数的查询用户信息存储过程:
CREATE PROCEDURE [dbo].[p_Select_User]
AS
BEGIN
SELECT * FROM tb_User
END
3>带参数有输出参数的存储过程:
CREATE PROCEDURE [dbo].[p_Select_UserCount]
@name nvarchar(50),
@result int output
AS
BEGIN
SELECT @result= COUNT(0) FROM tb_User WHERE @name=UserName
END
4>做好准备工作之后新建java项目,导入sqljdbc.jar
package com.Project_DataBase01;
import java.sql.Connection;
import java.sql.DriverManager;
public class SelectQuery {
private Connection conn;
/*
* 创建一个返回Connection的方法
*/
public Connection getConnection(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("数据库驱动加载成功");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=java_conn_test","sa","123456");
if(conn==null){
System.out.println("数据库连接失败");
System.out.println("-----------------------");
}else {
System.out.println("数据库连接成功");
System.out.println("-----------------------");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
}
5>执行存储过程:
package com.Project_DataBase01;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Types;
public class StartMain {
private static Connection conn;
public static void main(String[] args) {
// TODO Auto-generated method stub
conn=new SelectQuery().getConnection();
GetProduseInsert();
GetProduseSelect02();
GetProduseSelect();
}
/*
* 执行SELECT无参数存储过程,查询数据
*/