码迷,mamicode.com
首页 > 数据库 > 详细

XML scriptlet 连接数据库

时间:2017-12-14 17:59:37      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:cti   man   htm   ati   连接   username   ora   cep   连接数   

 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>

 

XML scriptlet 连接数据库

标签:cti   man   htm   ati   连接   username   ora   cep   连接数   

原文地址:http://www.cnblogs.com/liuyangv/p/8038559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!