java ee怎样从数据库读取数据并显示出来
答案:2 悬赏:0 手机版
解决时间 2021-11-25 08:48
- 提问者网友:眉目添风霜
- 2021-11-24 18:26
java ee怎样从数据库读取数据并显示出来
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-11-24 19:43
Java读取数据库,首先要将数据库的驱动包导入:如果是mysql数据库,需要导入mysql-connector-java-5.1.27.jar,如果是oracle就需要导入oracle.jar包;
然后就是建立数据库连接,使用Connection 新建连接,然后执行你的sql语句查询出需要的数据,下面是两个方法:
public PageModel findAllUser(int pageNo,int pageSize){
String sql = "select * from user where user_name <> 'root' order by user_name" +
"limit" + (pageNo - 1) * pageSize + "," + pageSize;
PageModel pageModel =null;
Connection conn =null;
Statement stmt =null;
ResultSet rs = null;
try{
conn = DB.getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
List user_List = new ArrayList();
while(rs.next()){
User user = new User();
user.setUser_name(rs.getString("user_name"));
user.setUser_password(rs.getString("user_password"));
user.setUser_tel(rs.getString("user_tel"));
user.setUser_email(rs.getString("user_email"));
user.setUser_createDate(rs.getTimestamp("user_createdate"));
user_List.add(user);
}
//取得所有记录
int totalRecords = getTotalRecords(conn);
pageModel = new PageModel();
pageModel.setPageSize(pageSize);
pageModel.setList(user_List);
pageModel.setTotalRecord(totalRecords);
}catch(SQLException e){
e.printStackTrace();
}finally{
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
return pageModel;
}
public int getTotalRecords(Connection conn){
String sql = "select count(*) from user where user_name <> 'root' ";
int totalRecords = 0;
Statement stmt = null;
ResultSet rs = null;
try{
conn = DB.getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){
totalRecords = rs.getInt(1);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
return totalRecords;
}
然后就是建立数据库连接,使用Connection 新建连接,然后执行你的sql语句查询出需要的数据,下面是两个方法:
public PageModel findAllUser(int pageNo,int pageSize){
String sql = "select * from user where user_name <> 'root' order by user_name" +
"limit" + (pageNo - 1) * pageSize + "," + pageSize;
PageModel pageModel =null;
Connection conn =null;
Statement stmt =null;
ResultSet rs = null;
try{
conn = DB.getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
List user_List = new ArrayList();
while(rs.next()){
User user = new User();
user.setUser_name(rs.getString("user_name"));
user.setUser_password(rs.getString("user_password"));
user.setUser_tel(rs.getString("user_tel"));
user.setUser_email(rs.getString("user_email"));
user.setUser_createDate(rs.getTimestamp("user_createdate"));
user_List.add(user);
}
//取得所有记录
int totalRecords = getTotalRecords(conn);
pageModel = new PageModel();
pageModel.setPageSize(pageSize);
pageModel.setList(user_List);
pageModel.setTotalRecord(totalRecords);
}catch(SQLException e){
e.printStackTrace();
}finally{
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
return pageModel;
}
public int getTotalRecords(Connection conn){
String sql = "select count(*) from user where user_name <> 'root' ";
int totalRecords = 0;
Statement stmt = null;
ResultSet rs = null;
try{
conn = DB.getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){
totalRecords = rs.getInt(1);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
return totalRecords;
}
全部回答
- 1楼网友:末日狂欢
- 2021-11-24 19:58
System.out.println("学生姓名:"+StuID); String CourNum=rs.getString(CourNum); System.out.println("课程编号:"+CourNum); String CourID=rs.getString(CourID); System.out.println("课程名称:"+CourID); String CourS=rs.getString(CourS); System.out.println("课程成绩:"+CourS); 这里不对。
满意请采纳
满意请采纳
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯