zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    IIS7中启用JS的压缩
    .NET中使用WINDOWS API参数定义
    IE6下的JQUERY_FCK兼容问题
    移动native+HTML5混搭应用感受
    【翻译】Fixie.js——自动填充内容的插件
    JSON辅助格式化
    【记录】导致notifyDataSetChanged无效的一个错误
    【Perl】批量word和PPT文档转pdf
    【记录】有关parseInt的讨论
    手机滑动应用
  • 原文地址:https://www.cnblogs.com/liuyangv/p/8038559.html
Copyright © 2011-2022 走看看