XML scriptlet 连接数据库

Posted Mr·Liu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

 

以上是关于XML scriptlet 连接数据库的主要内容,如果未能解决你的问题,请参考以下文章

JSP语法

JSP 语法

JSP开发中的基础语法

那些你常用的JSP知识

JSP 基础语法

jsp语法简介