批处理和存储过程
2016-08-21 17:40
169 查看
批处理:一次执行多条sql语句
package com.heima.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Date;
import org.junit.Test;
import com.heima.utils.JdbcUtils;
public class BatchTest {
// 通过Statement命令对象演示一个插入3条记录
@Test
public void testStatement() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
// 创建预处理命令对象
Statement stmt = conn.createStatement();
String sql1 = "insert into t3 values(1,'令狐冲',20)";
String sql2 = "insert into t3 values(2,'杨过',23)";
String sql3 = "insert into t3 values(3,'乔峰',24)";
// 将三条sql语句一起放入stmt对象中,一起发送到服务执行,在DBMS中会编译成一个逻辑执行单元,所以速度会更快一些
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
// 执行sql语句
stmt.executeBatch();
// 释放资源
JdbcUtils.release(null, stmt, conn);
}
// 通过PreparedStatement对象插入三条数据
@Test
public void testPreparedStatement() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
// 创建预处理命令对象
PreparedStatement pstmt = conn
.prepareStatement("insert into t3 values(?,?,?)");
// 指定?的值
pstmt.setInt(1, 4);
pstmt.setString(2, "黄蓉");
pstmt.setInt(3, 18);
pstmt.addBatch();
// 指定?的值
pstmt.setInt(1, 5);
pstmt.setString(2, "小龙女");
pstmt.setInt(3, 17);
pstmt.addBatch();
// 指定?的值
pstmt.setInt(1, 6);
pstmt.setString(2, "赵敏");
pstmt.setInt(3, 18);
pstmt.addBatch();
// 执行sql语句
pstmt.executeBatch();
// 释放资源
JdbcUtils.release(null, pstmt, conn);
}
// 通过PreparedStatement对象插入1000条数据
@Test
public void testPreparedStatement1() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
System.out.println(new Date());
// 创建预处理命令对象
PreparedStatement pstmt = conn
.prepareStatement("insert into t3 values(?,?,?)");
for (int i = 0; i < 1000; i++) {
// 指定?的值
pstmt.setInt(1, i);
pstmt.setString(2, "黄蓉" + i);
pstmt.setInt(3, i);
pstmt.addBatch();
if(i%200 == 0){
//执行sql语句
pstmt.executeBatch() ;
//一定要清空缓存
pstmt.clearBatch() ;
}
}
//为了防止缓存中还有sql没有执行,应当再次 执行sql语句
pstmt.executeBatch();
System.out.println(new Date());
// 释放资源
JdbcUtils.release(null, pstmt, conn);
}
}
存储过程:
相当于一个方法
在数据库中定义一个方法
返回值叫输出参数
DELIMITER && 用 DELIMITER 规定一个结束的标志(相当于分号)
CREATE PROCEDURE pro1()
begin
select * from stu;
end;
$$
调用:
call pro1()
$$
#无参的存储过程
delimiter $$
create procedure pro1()
begin
select * from stu ;
end ;
$$
#带输入参数的存储过程
create procedure pro2(in a int)
begin
select * from stu where id = a ;
end ;
$$
delimiter $$
create procedure pro3(in a int,in b int)
begin
select a + b ;
end ;
$$
delimiter ;
delimiter $$
create procedure pro4(in n int)
begin
select * from t3 where id = n ;
end ;
$$
delimiter ;
#创建带输出参数的存储过程
delimiter $$
create procedure pro5(in a int,out b varchar(20))
begin
select name into b from stu where id = a ;
end ;
$$
call pro5(1,@n) ;
select @n ;
#创建带多个输出参数的存储过程
delimiter $$
create procedure pro6(in a int,out b varchar(20),out c int)
begin
select name,age into b,c from stu where id = a ;
end ;
$$
select @name,@age $$
package com.heima.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import org.junit.Test;
import com.heima.utils.JdbcUtils;
//演示JDBC调用存储过程
public class ProcedureTest {
//执行不带返回值的存储过程没有任何任意
@Test
public void testPro2() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
// 创建执行存储过程的命令对象
CallableStatement cstmt = conn.prepareCall("{call pro2(?)}") ;
//指定?的值
cstmt.setInt(1, 1) ;
//执行存储过程
cstmt.execute() ;
// 释放资源
JdbcUtils.release(null, cstmt, conn);
}
@Test
public void testPro5() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
// 创建执行存储过程的命令对象
CallableStatement cstmt = conn.prepareCall("{call pro5(?,?)}") ;
//指定?的值
cstmt.setInt(1, 1) ;
//指定第二个?是输出参数
cstmt.registerOutParameter(2, Types.VARCHAR) ;
//执行存储过程
cstmt.execute() ;
//获得返回值
String name = cstmt.getString(2) ;
System.out.println(name);
// 释放资源
JdbcUtils.release(null, cstmt, conn);
}
}
获取数据库自动生成的主键:
package com.heima.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Date;
import org.junit.Test;
import com.heima.utils.JdbcUtils;
public class BatchTest {
// 通过Statement命令对象演示一个插入3条记录
@Test
public void testStatement() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
// 创建预处理命令对象
Statement stmt = conn.createStatement();
String sql1 = "insert into t3 values(1,'令狐冲',20)";
String sql2 = "insert into t3 values(2,'杨过',23)";
String sql3 = "insert into t3 values(3,'乔峰',24)";
// 将三条sql语句一起放入stmt对象中,一起发送到服务执行,在DBMS中会编译成一个逻辑执行单元,所以速度会更快一些
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
// 执行sql语句
stmt.executeBatch();
// 释放资源
JdbcUtils.release(null, stmt, conn);
}
// 通过PreparedStatement对象插入三条数据
@Test
public void testPreparedStatement() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
// 创建预处理命令对象
PreparedStatement pstmt = conn
.prepareStatement("insert into t3 values(?,?,?)");
// 指定?的值
pstmt.setInt(1, 4);
pstmt.setString(2, "黄蓉");
pstmt.setInt(3, 18);
pstmt.addBatch();
// 指定?的值
pstmt.setInt(1, 5);
pstmt.setString(2, "小龙女");
pstmt.setInt(3, 17);
pstmt.addBatch();
// 指定?的值
pstmt.setInt(1, 6);
pstmt.setString(2, "赵敏");
pstmt.setInt(3, 18);
pstmt.addBatch();
// 执行sql语句
pstmt.executeBatch();
// 释放资源
JdbcUtils.release(null, pstmt, conn);
}
// 通过PreparedStatement对象插入1000条数据
@Test
public void testPreparedStatement1() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
System.out.println(new Date());
// 创建预处理命令对象
PreparedStatement pstmt = conn
.prepareStatement("insert into t3 values(?,?,?)");
for (int i = 0; i < 1000; i++) {
// 指定?的值
pstmt.setInt(1, i);
pstmt.setString(2, "黄蓉" + i);
pstmt.setInt(3, i);
pstmt.addBatch();
if(i%200 == 0){
//执行sql语句
pstmt.executeBatch() ;
//一定要清空缓存
pstmt.clearBatch() ;
}
}
//为了防止缓存中还有sql没有执行,应当再次 执行sql语句
pstmt.executeBatch();
System.out.println(new Date());
// 释放资源
JdbcUtils.release(null, pstmt, conn);
}
}
存储过程:
相当于一个方法
在数据库中定义一个方法
返回值叫输出参数
DELIMITER && 用 DELIMITER 规定一个结束的标志(相当于分号)
CREATE PROCEDURE pro1()
begin
select * from stu;
end;
$$
调用:
call pro1()
$$
#无参的存储过程
delimiter $$
create procedure pro1()
begin
select * from stu ;
end ;
$$
#带输入参数的存储过程
create procedure pro2(in a int)
begin
select * from stu where id = a ;
end ;
$$
delimiter $$
create procedure pro3(in a int,in b int)
begin
select a + b ;
end ;
$$
delimiter ;
delimiter $$
create procedure pro4(in n int)
begin
select * from t3 where id = n ;
end ;
$$
delimiter ;
#创建带输出参数的存储过程
delimiter $$
create procedure pro5(in a int,out b varchar(20))
begin
select name into b from stu where id = a ;
end ;
$$
call pro5(1,@n) ;
select @n ;
#创建带多个输出参数的存储过程
delimiter $$
create procedure pro6(in a int,out b varchar(20),out c int)
begin
select name,age into b,c from stu where id = a ;
end ;
$$
select @name,@age $$
package com.heima.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import org.junit.Test;
import com.heima.utils.JdbcUtils;
//演示JDBC调用存储过程
public class ProcedureTest {
//执行不带返回值的存储过程没有任何任意
@Test
public void testPro2() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
// 创建执行存储过程的命令对象
CallableStatement cstmt = conn.prepareCall("{call pro2(?)}") ;
//指定?的值
cstmt.setInt(1, 1) ;
//执行存储过程
cstmt.execute() ;
// 释放资源
JdbcUtils.release(null, cstmt, conn);
}
@Test
public void testPro5() throws Exception {
// 获得链接对象
Connection conn = JdbcUtils.getConnection();
// 创建执行存储过程的命令对象
CallableStatement cstmt = conn.prepareCall("{call pro5(?,?)}") ;
//指定?的值
cstmt.setInt(1, 1) ;
//指定第二个?是输出参数
cstmt.registerOutParameter(2, Types.VARCHAR) ;
//执行存储过程
cstmt.execute() ;
//获得返回值
String name = cstmt.getString(2) ;
System.out.println(name);
// 释放资源
JdbcUtils.release(null, cstmt, conn);
}
}
获取数据库自动生成的主键:
相关文章推荐
- .net下 存储过程、事务处理 的一个小例子
- MSSQL的高危存储过程及其依存DLL的安全处理
- SQLServer-存储过程中xml的处理
- 批处理调用存储过程
- 处理带参数的存储过程
- 模拟字符串处理函数 stuff 的存储过程,对 ntext 字段进行stuff
- SQLServer下 存储过程内 包含事务 及 返回处理是否成功
- dotnet中调用存储过程的用法,不含事务处理
- SQLServer-存储过程中的简单事务处理
- 个人学习代码保存:例8.在存储过程中使用简单的事务处理
- 存储过程 分页处理
- 存储过程事务处理
- 关于多结果集的存储过程的处理
- SQL Server 2000 处理操作系统注册表的系统存储过程
- 触发器、存储过程和事务处理使用
- 处理孤立用户的存储过程
- 存储过程中null值的处理
- 使用数据2分处理的通用分页存储过程 前半部分与后半部分数据访问时间相同
- dotnet中调用存储过程的用法,包含事务处理
- 因为不想再程序中写循环,因此把UI页面需要更新的纪录的主键组成了一个以","隔开的字符串,将这个字符串传入存储过程中进行处理(转)