eResultSet(rs);
ConnectionUtils.closeStatement(pStmt);
ConnectionUtils.closeConnection(conn);
}
return flights;
}
public static void main(String[] args) throws Exception {
List ff = new FlightDaoImplForMySQL().
listPagesNewsDB(2, 3,
ConnectionUtils.openConnection());
for(Flight f:ff){
System.out.println(“==”+f.getId()+”=”+f.getNum()+”==”);
}
}
}
——————————————————————————-
//Oracle 中的分页查询 实现
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import week07.domain.News;
import week07.util.ConnectionUtils;
public class NewsDaoImpl implements INewsDao {
/**
* 分页查询news表的所有列的数据
*
* @param page
* 第几页
* @param rowsPerPages
* 每页显示行数
* @param con
* 数据库连接
* @return 查询到的数据以News对象的形式存储到List中返回
* @throws Exception
* select news_id, title, content, author, pubDate, rn from(
* select news_id, title, content, author, pubDate, rownum rn
* from news
* where rownum < )
* where rn >= ;
*/
public List listPagesNewsDB(int page, int rowsPerPage,
Connection con) throws Exception {
int from = (page – 1) * rowsPerPage + 1;
int to = from + rowsPerPage;
String sql = “select news_id, title, ”
+ “content, author, pubDate, rn ” + “from ”
+ “(select news_id, title, content, ”
+ “author, pubDate, rownum rn ”
+ “from news where rownum < ) ” + “where rn >= ”;
Connection conn = null;
PreparedStatement pStmt = null;
ResultSet rs = null;
News news = null;
List newses = new ArrayList();
try {
conn = ConnectionUtils.openConnection();
pStmt = conn.prepareStatement(sql);
pStmt.setInt(1, to);
pStmt.setInt(2, from);
rs = pStmt.executeQuery();
while (rs.next()) {
news = new News();
news.setNewsId(rs.getLong(1));
news.setTitle(rs.getString(2));
news.setContent(rs.getString(3));
news.setAuthor(rs.getString(4));
news.setPubdate(rs.getDate(5));
newses.add(news);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtils.closeResultSet(rs);
ConnectionUtils.closeStatement(pStmt);
ConnectionUtils.closeConnection(conn);
}
return newses;
}
}
17、JDBC,Hibernate 分页怎样实现?
答:方法分别为:
1) Hibernate 的分页:
Query query = session.createQuery(“from Student”);
query.setFirstResult(firstResult);//设置每页开始的记录号
query.setMaxResults(resultNumber);//设置每页显示的记录数
Collection students = query.list();
2) JDBC 的分页:根据不同的数据库采用不同的sql 分页语句
例如: Oracle 中的sql 语句为: “SELECT * FROM (SELECT a.*, rownum r FROM
TB_STUDENT) WHERE r between 2 and 10″ 查询从记录号2 到记录号10 之间的所有记录
倒三角
public class daosanjiao {
public static void main(String[] args) {
for(int i=0;i<4;i++)
{
for(int k=0;k
{
System.out.print(” “);
}
for(int j=0;j<7-2*i;j++)
{
System.out.print(“*”);
}
System.out.println();
}
}
}
42、用你熟悉的语言写一个连接ORACLE数据库的程序,能够完成修改和查询工作。 答:JDBC示例程序
如下: public void testJdbc(){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
//step1:注册驱动;
Class.forName(“oracle.jdbc.driver.OracleDriver”);
//step 2:获取数据库连接;
con=DriverManager.getConnection(
“jdbc:oracle:thin:@192.168.0.23:1521:tarena”, “openlab”,”open123″);
/************************查 询************************/
//step 3:创建Statement;
String sql = “SELECT id, fname, lna