1.JDBC 的批处理
package cj;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BatchTest {
public static void main(String[] args) throws SQLException {
createBatch();
}
public static void createBatch()throws SQLException{
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
con=JdbcUtil.getConnection();
String sql="insert into person(id,name,birthday,money)values(?,?,?,?)";
ps=con.prepareStatement(sql);
for(int i=0;i<100;i++){
ps.setInt(1, i);
ps.setString(2, "name"+i);
ps.setDate(3, new Date(System.currentTimeMillis()));
ps.setInt(4, i);
ps.addBatch();
}
ps.executeBatch();
}finally {
JdbcUtil.free(rs, ps, con);
}
}
}
ps.addBatch()---相当于打包
ps.executeBatch()---将包里面的数据一起处理
2.分页
ResultSet设置一些参数以后可以进行向前向后移动
st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
TYPE_SCROLL_SENSITIVE
The constant indicating the type for a ResultSet
object that is scrollable and generally sensitive to changes to the data that underlies the ResultSet
.
CONCUR_READ_ONLY
The constant indicating the concurrency mode for a ResultSet
object that may NOT be updated.
设置这些参数后就可以得到滚动的结果集。
1.取得结果集前面的值
if(rs.previous()){
System.out.print(rs.getObject("name") + "\t"+ rs.getObject("id"));
System.out.println();
}
2.将结果集定位到某个地方
rs.absolute(10);
3.分页查询
int i=0;
rs.absolute(10);
while (rs.next()&&i<10) {
i++;
System.out.print(rs.getObject("name") + "\t"+ rs.getObject("id"));
System.out.println();
}
先定位到10的位置,然后取得后面的十条语句。
分页不是所有的数据库都要求这样处理,对于像mysql这样的数据库直接用limit就可以实现分页。
select id,name from person limit 100,10
先定位到100条的位置然后在取得10条记录。