10月25日学习日志

今天学习了Java数据库操作对数据的现实方法。

代码如:

public static Integer getAll() {
   Connection conn = getConn();
   String sql = "select * from students";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement)conn.prepareStatement(sql);
       ResultSet rs = pstmt.executeQuery();
       int col = rs.getMetaData().getColumnCount();
       System.out.println("============================");
       while (rs.next()) {
           for (int i = 1; i <= col; i++) {
               System.out.print(rs.getString(i) + "\t");
               if ((i == 2) && (rs.getString(i).length() < 8)) {
                   System.out.print("\t");
               }
            }
           System.out.println("");
       }
           System.out.println("============================");
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return null;
}

 

posted @ 2020-10-25 15:01  张笑天  阅读(41)  评论(0编辑  收藏  举报