java自学之路-----jdbc_分页查询,大数据,批处理,存储过程
分页查询{ 实际上就是每次查询一部分记录,并显示: select * from table_name limit StartIndex, PageSize;StartIndex:从第几个记录开始查。PageSize:每次查多少记录 分页查询在javaweb中的案例: //该对象封装了分页查询的配置信息,用于dao层public cla
分页查询{
实际上就是每次查询一部分记录,并显示:
select * from table_name limit StartIndex, PageSize;——>StartIndex:从第几个记录开始查。PageSize:每次查多少记录
分页查询在javaweb中的案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//该对象封装了分页查询的配置信息,用于dao层
public
class
QueryInfo {
private
int startindex;
//查询的每页在数据库的起始位置,该值可以根据其他参数计算出,不需要set属性
private
int pagesize = 5;
//查询的每页记录数,赋一个初始值
private
int querypage = 1;
//查询第几页,赋一个初始值
//提供get,set方法,便于设置,获取属性值
public
int getStartindex() {
this.startindex = (this.querypage-1) + 1;
return
startindex;
}
public
int getPagesize() {
return
pagesize;
}
public
void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public
int getQuerypage() {
return
querypage;
}
public
void setQuerypage(int querypage) {
this.querypage = querypage;
}
}
Copy after login
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//该类封装了分页查询的结果信息,作为中间值,用于给QueryBean提供信息
public
class
QueryResult {
private
List list;
//封装了查询后某页的所有记录
private
int totalrecord;
//总记录数,用于给用户提供交互信息
// 提供属性的get,set方法,
public
List getList() {
return
list;
}
public
void setList(List list) {
this.list = list;
}
public
int getTotalrecord() {
return
totalrecord;
}
public
void setTotalrecord(int totalrecord) {
this.totalrecord = totalrecord;
}
}
Copy after login
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//该类封装了分页查询的所有信息,用于与用户交互,用于jsp页面,web层
public
class
QueryBean {
private
List list;
//封装查询的所有记录信息
private
int totalrecord;
//告诉用户总共有多少记录,可以从QueryResult对象获取
private
int pagesize;
//告诉用户每页有多少记录,从QueryInfo对象获取
private
int totalpage;
//告诉用户一共有多少页,可以通过totalrecord和pagesize计算
private
int currentpage;
//告诉用户当前查看的是第几页,从QueryInfo对象获取
private
int previouspage;
//上一页是第几页,通过当前页计算出
private
int nextpage;
//下一页是第几页,通过当前页计算出
private
int[] pagebar;
//记录页码条,通过currentpage和pagesize计算出的一个数组
// 提供所有属性的get,set方法
public
int getTotalrecord() {
return
totalrecord;
}
public
void setTotalrecord(int totalrecord) {
this.totalrecord = totalrecord;
}
public
int getPagesize() {
return
pagesize;
}
public
void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public
int getTotalpage() {
// 计算总页数,21条,每页5条,有5页。20条,每页5条,有4页。
if
(this.totalrecord%this.pagesize == 0){
this.totalpage = this.totalrecord / this.pagesize;
}
else
{
this.totalpage = this.totalrecord / this.pagesize + 1;
}
return
totalpage;
}
public
int getCurrentpage() {
return
currentpage;
}
public
void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public
int getPreviouspage() {
this.previouspage = this.currentpage - 1;
// 如果当前页小于等于0,就指向第一页
if
(this.previouspage<=0){
this.previouspage = 1;
}
return
previouspage;
}
public
int getNextpage() {
this.previouspage = this.currentpage - 1;
// 如果当前页小于等于0,就指向最后一页
if
(this.nextpage<=0){
this.nextpage = 1;
}
return
nextpage;
}
public
int[] getPagebar() {
int startindex;
int endindex;
// 如果总页数小于10
if
(this.pagesize <= 10){
startindex = 1;
endindex = this.pagesize;
}
else
{
startindex = this.currentpage - 4;
endindex = this.currentpage + 5;
// 如果startindex小于1,就从0开始,如果endindex大于总页数,就用总页数作为结束
if
(startindex < 1){
startindex = 1;
endindex = 10;
}
if
(endindex > this.pagesize){
startindex = this.pagesize - 9;
endindex = this.pagesize;
}
}
// 更具两个索引值定义数组pagebar
this.pagebar =
new
int[endindex-startindex+1];
// 为数组赋值
for
(int i = 0; i < pagebar.length; i++) {
this.pagebar[i] = startindex++;
}
return
pagebar;
}
}
Copy after login
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 在dao层提供一个分页查询的方法,将索引的初始位置和每页记录数传入
public
QueryResult pageQuery(int startIndex, int pageSize){
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try
{
// 获取数据库连接
conn = JdbcUtils.getConnection();
// 提供分页查询的sql语句
String sql =
"select * from customer limit ?,?"
;
// 预编译sql语句,获得preparedstatement对象
st = conn.prepareStatement(sql);
// 为占位符'?' 赋值
st.setInt(1, startIndex);
st.setInt(2, pageSize);
// 执行sql语句,获得结果集
rs = st.executeQuery();
// 将结果封装到QueryResult对象中
//在service层,将该对象封装到QueryBean中,传给jsp用于显示
...
}
catch
(Exception e) {
}finally{
JdbcUtils.release(conn, st, rs);
}
}
Copy after login
}
大数据{
用处:程序需要将大文本或二进制数据保存到数据库中,一般在实际开发中不直接使用数据库存储大文本大数据,因为在存取操作的时候,会占用连接大量的时间。
大数据(Large Objects):
clob:存储大文本,text文件blob:存储二进制数据,如图像,音频、二进制文mysql中用Text代替clob:
Text:Tinytext,text,mediumtext,longtext
blob:tinyblob,blob,mediumblob,longblob
大文本(Text){
存:
PreparedStatement的setCharacterStream( 1, reader, length)方法,填充占位符
1 2 3 4 5 6 7 8 9 10 11 |
|
读:
reader = resultSet.getCharacterStream(i);
reader = resultSet.get Clob(i).getCharacterStream();
String s = resultSet.getSting(i);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 定义sql语句
String sql =
"select * from testclob where id= '1'"
;
// 预编译sql
PreparedStatement st = conn.prepareStatement(sql);
// 无占位符直接执行sql
ResultSet rs = st.executeQuery();
// 通过结果集获取大数据文件
while
(rs.next()){
String id = rs.getString(1);
// 将数据库的文本数据放在一个流中,供用户使用
Reader reader = rs.getCharacterStream(2);
// 读取数据
Writer writer =
new
FileWriter(
"src/2.txt"
);
...
}
Copy after login
}
二进制(Blob){
存:
PreparedStatement的setBinaryStream(i, inputStream, length)方法,填充占位符
1
2
3
4
5
6
7
8
9
10
11
// 定义sql语句
String sql =
"insert into testblob(id,image) values(?,?)"
;
// 预编译sql
PreparedStatement st = conn.prepareStatement(sql);
// 赋值占位符
st.setString(1,
"1"
);
// 不建议用类装载器读取文件,因为文本内容可能太大,不适合读取至内存中
File file =
new
File(
"src/1.jpg"
);
InputStream inputStream =
new
FileInputStream(file);
st.setBinaryStream(2, inputStream, file.length());
//inputStream:一个读取文本的流 length:文本的大小
// 这是long型的,使用1.6一下的jdk必须将其转成int
Copy after login读:查看数据不能直接通过命令行程序读取
inputStream = resultSet.getBinaryStream(i);
inputStream = resultSet.getBlob(i).getBinaryStream();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 定义sql语句
String sql =
"select * from testblob where id= '1'"
;
// 预编译sql
PreparedStatement st = conn.prepareStatement(sql);
// 无占位符直接执行sql
ResultSet rs = st.executeQuery();
// 通过结果集获取大数据文件
while
(rs.next()){
String id = rs.getString(1);
// 将数据库的文本数据放在一个流中,供用户使用
InputStream in = rs.getBinaryStream(2);
// 存储数据
OutputStream out =
new
FileOutputStream(
"c:\\2.jpg"
);
...
Copy after login
}
}
批处理{
用处:当需要向数据库发送一批SQL语句执行时,应避免向数据库中一条条的发送执行,应该采用批处理机制,提升效率
第一种实现方式{
存储sql语句:Statement.addBatch(sql);
执行批处理语句:executeBatch()-->执行批处理命令 clearBatch();-->清除批处理命令
1
2
3
4
5
6
7
8
9
10
11
12
// 定义sql语句
String sql1 =
"insert into testBatch(id,name) values('1', 'aaa')"
;
String sql2 =
"update testBatch set name = 'bbb' where id = '1'"
;
// 获取Statement对象
Statement st = conn.createStatement();
// 将sql语句存到对象中,实际是将sql语句存放在内部的list对象中
st.addBatch(sql1);
st.addBatch(sql2);
// 执行sql
int result[] = st.executeBatch();
//该数组一次存放着每一条sql语句影响数据库数据的行数
// 清除
st.clearBatch();
Copy after login
1.可以发送不同的sql语句
2.性能不够
}
第二种实现方式{
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 定义sql语句
String sql =
"insert into testBatch(id,name) values(?,?)"
;
// 获取PreparedStatement对象
PreparedStatement st = conn.prepareStatement(sql);
//// 替换占位符,第一个sql,存到list中
// st.setString(1, "1");
// st.setString(2, "aaa");
// st.addBatch();
//// 替换占位符,第二个sql,存到list中
// st.setString(1, "2");
// st.setString(2, "bbb");
// st.addBatch();
//例:
for
(int i=0; i<10; i++){
st.setString(1,
""
+i);
st.setString(2,
"aa"
+i);
st.addBatch();
}
// 执行
st.executeBatch();
// 清除
st.clearBatch();
Copy after login
1.只能发送相同sql语句,比较适合作为批量插入或批量更新
2.预先编译,提升性能
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
}
存储过程{
该对象作用:
1.相当于数据库的一个方法,在数据库中处理数据,然后再java程序中直接获取数据
2.没有该对象就需要使用jdbc调用来处理数据
3.一般都用在金融证券领域,都内部写了存储过程,程序员直接调用。
4.存储过程中一般在参数中指定了输入数据,和输出数据
用法:
1.先在数据库中注册一个存储过程
2.在java程序中使用
1
2
3
4
5
6
7
8
9
10
11
12
// 获取连接,通过数据库的方法,先得到一个数据,处理后再输出
Connection conn = JdbcUtils.getConnection();
// 用存储过程名来获取存储过程的对象
CallableStatement st = conn.prepareCall(
"{call demoSp(?,?)}"
);
//通过demoSp这个名字调用存储过程
// 替换占位符
//输入的数据
st.setString(1,
"aaa"
);
//输出的数据,提供输出的类型
st.registerOutParameter(2, Types.VARCHAR);
//Types封装了数据库中常用类型
st.execute();
// 通过第二个?获得数据,并且它已经指定了数据类型
st.getString(2);
Copy after login
问题:1.只知道存储过程在java程序中的输入,输出关系。不知道会对数据库造成怎样的影响 2.不知道怎么编写存储过程
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
