2、分页显示数据
分頁顯示
public class jdbcTest {public static void main(String[] args) throws SQLException {Connection con=getConnection("root", "root");// 獲取數(shù)據(jù)庫連接paging(con, 1,2);//方法名調(diào)用數(shù)據(jù)庫連接,且定義顯示第幾行到第幾行releaseConnection(con);// 釋放數(shù)據(jù)庫連接}/*** 分頁查詢* @param con* @param startIndex* @param total*/public static void paging(Connection con,int startIndex,int total){try{String sql="select * from student limit ?,?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setInt(1, startIndex);pstmt.setInt(2, total);ResultSet rs=pstmt.executeQuery();while(rs.next()){System.out.println("學(xué)生ID:"+rs.getInt("stuId")+",學(xué)生姓名:"+rs.getString("stuName")+",學(xué)生性別:"+rs.getString("stuSex")+",學(xué)生年齡:"+rs.getInt("stuAge"));}rs.close();pstmt.close();}catch(Exception e){e.printStackTrace();}}/*** 數(shù)據(jù)庫連接* @param dbUserName* @param dbPassword* @return* @throws SQLException*/public static Connection getConnection(String dbUserName,String dbPassword) throws SQLException{Connection con=null;//聲明連接對象String jdbcName="com.mysql.jdbc.Driver";//驅(qū)動程序類名String dbUrl="jdbc:mysql://localhost:3306/book?"//數(shù)據(jù)庫Url+"useUnicode=true&characterEncoding=UTF8";//防止亂碼try {Class.forName(jdbcName);con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);//獲取數(shù)據(jù)庫連接} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return con;}/*** 釋放數(shù)據(jù)庫連接* @param con*/public static void releaseConnection(Connection con){try{if(con!=null){con.close();}}catch(Exception e){e.printStackTrace();}} }
總結(jié)
- 上一篇: 8、使用元数据(描述数据属性的信息)分析
- 下一篇: 1、项目演示