如何从mysql数据库读取数据
1个回答
展开全部
读取数据库,首先要将数据库的驱动包导入:如果是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;} /** * 取得所有记录 * @param conn * @return 所有记录totalRecords */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;} /** * 取得所有记录 * @param conn * @return 所有记录totalRecords */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;}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询