• JavaBean


    JavaBean的一个重要的应用就是将JSP中的JDBC数据库查询代码移到JavaBean
    用于访问数据库的Java类,被称为DAO
    查询出了每一条对象被称为VO
     
     1 package dao;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.ResultSet;
     6 import java.sql.SQLException;
     7 import java.sql.Statement;
     8 import java.util.ArrayList;
     9 import beans.Student;
    10 
    11 public class StudentDao {
    12     public ArrayList queryAllStudents() throws Exception {
    13         Connection conn = null;
    14         ArrayList students = new ArrayList();
    15         try {
    16             // 获取连接
    17             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    18             String url = "jdbc:odbc:DsSchool";
    19             conn = DriverManager.getConnection(url, "", "");
    20             // 运行SQL语句
    21             String sql = "SELECT STUNO,STUNAME from T_STUDENT";
    22             Statement stat = conn.createStatement();
    23             ResultSet rs = stat.executeQuery(sql);
    24             while (rs.next()) {
    25                 // 实例化VO
    26                 Student student = new Student();
    27                 student.setStuno(rs.getString("STUNO"));
    28                 student.setStuname(rs.getString("STUNAME"));
    29                 students.add(student);
    30             }
    31             rs.close();
    32             stat.close();
    33         } catch (SQLException e) {
    34             e.printStackTrace();
    35         } finally {
    36             try {// 关闭连接
    37                 if (conn != null) {
    38                     conn.close();
    39                     conn = null;
    40                 }
    41             } catch (Exception ex) {
    42             }
    43         }
    44         return students;
    45     }
    46 }
     1 <%@ page language="java" import="java.util.*,java.sql.*"
     2     pageEncoding="gb2312"%>
     3 <%@page import="dao.StudentDao"%>
     4 <%@page import="beans.Student"%>
     5 <html>
     6 <body>
     7     <%
     8         StudentDao studentDao = new StudentDao();
     9         ArrayList students = studentDao.queryAllStudents();
    10     %>
    11     <table border=2>
    12         <tr>
    13             <td>学号</td>
    14             <td>姓名</td>
    15         </tr>
    16         <%
    17             for (int i = 0; i < students.size(); i++) {
    18                 Student student = (Student) students.get(i);
    19         %>
    20         <tr>
    21             <td><%=student.getStuno()%></td>
    22             <td><%=student.getStuname()%></td>
    23         </tr>
    24         <%
    25             }
    26         %>
    27     </table>
    28 </body>
    29 </html>
  • 相关阅读:
    正则表达式学习网站
    Longest Substring Without Repeating Characters
    保留小数点后几位数字
    ReentrantLock和synchronized的区别随笔
    范型 小编
    两个线程交替打印字符串
    Gray Code
    Ajax
    堆排序
    Clone Graph
  • 原文地址:https://www.cnblogs.com/lnas01/p/4492257.html
Copyright © 2020-2023  润新知