XML scriptlet 连接数据库

 1 <%@ page language="java" contentType="text/html" pageEncoding="GBK" %>
 2 <%@ page import="java.sql.*" %>
 3 
 4 <html>
 5     <head>
 6         <title>emp列表</title>
 7         
 8         
 9     </head>
10     <body>
11         <%!
12             public static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
13             public static final String DBURL="jdbc:oracle:thin:@localhost:1521:orcl";
14             public static final String DBUSERNAME="scott";
15             public static final String DBPASSWORD="orcl";
16         %>
17         <%
18             Connection conn = null;
19             PreparedStatement ps = null;
20             ResultSet rs = null;
21             try{
22                 Class.forName(DBDRIVER);
23                 conn = DriverManager.getConnection(DBURL,DBUSERNAME,DBPASSWORD);
24                 String sql="select empno,ename,job,sal,hiredate from emp";
25                 ps = conn.prepareStatement(sql);
26                 rs = ps.executeQuery();
27                 
28         %>
29                 <table border="1" width="80%">
30                     <tr bgcolor="red">
31                         <td>员工编号</td>
32                         <td>员工姓名</td>
33                         <td>员工职位</td>
34                         <td>员工薪资</td>
35                         <td>入职日期</td>
36                     </tr>
37                 <%
38                     while(rs.next()){
39                         int empno=rs.getInt(1);
40                         String ename=rs.getString(2);
41                         String job=rs.getString(3);
42                         int sal=rs.getInt(4);
43                         java.util.Date hiredate=rs.getDate(5);
44                 %>        
45                     <tr bgcolor="gray">
46                         <td> <%=empno %> </td>
47                         <td> <%=ename %> </td>
48                         <td> <%=job %> </td>
49                         <td> <%=sal %> </td>
50                         <td> <%=hiredate %> </td>
51                     </tr>
52                     <%
53                     }
54                     %>    
55                 </table>
56                 
57         <%    
58             }catch(Exception e){
59                 //抛异常
60                 System.out.println(e);
61             }finally{
62                 if(rs!=null){
63                     rs.close();
64                 }
65                 if(ps!=null){
66                     ps.close();
67                 }
68                 if(conn!=null){
69                     conn.close();
70                 }    
71             }
72         %>
73             
74     </body>
75 </html>

 

posted @ 2017-12-14 16:50  Mr·Liu  阅读(224)  评论(0编辑  收藏  举报