• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Jsp访问数据库

开发技术 开发技术 10个月前 (04-05) 54次浏览
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10        <form action="check.jsp" method="post">
11         用户名:<input type="text" name="uname"/><br>
12        密码:<input type="password" name="upwd"/><br>
13        <input type="submit" value="登录"/><br>    
14        </form>
15 </body>
16 </html>
17 
18 
19 
20 
21 
22 <%@ page language="java" contentType="text/html; charset=UTF-8"
23     pageEncoding="UTF-8"%>
24     <%@ page import="java.sql.*" %>
25 <!DOCTYPE html>
26 <html>
27 <head>
28 <meta charset="UTF-8">
29 <title>Insert title here</title>
30 </head>
31 <body>
32 <%
33             String URL="jdbc:oracle:thin:@localhost:1521:ORCL";
34             String USERNAME="scott";
35             String PWD="tiger";
36             String className="oracle.jdbc.OracleDriver";
37             Connection con=null;
38             PreparedStatement pstmt=null;
39             ResultSet rs=null;
40             
41             try {
42                 Class.forName("oracle.jdbc.OracleDriver");
43                 con=DriverManager.getConnection(URL, USERNAME, PWD);
44                 
45                
46                 String name = request.getParameter("uname") ;
47                 String pwd = request.getParameter("upwd") ;
48                 
49                 
50                 String sql ="select count(*) from login where uname= ? and upwd =?"; 
51                 pstmt = con.prepareStatement(sql) ;
52                 pstmt.setString(1, name);
53                 pstmt.setString(2, pwd);
54                 rs = pstmt.executeQuery();
55                 
56                 // d.处理结果
57                 int count = -1;
58                 if(rs.next()) {
59                     count = rs.getInt(1);
60                 }
61                 if(count>0) {
62                     out.println("登陆成功!");
63                 }else {
64                     out.println("登陆失败!");
65                     out.println(count);
66                 }
67             
68             } catch (ClassNotFoundException e) {
69                 // TODO Auto-generated catch block
70                 e.printStackTrace();
71             } catch (SQLException e) {
72                 // TODO Auto-generated catch block
73                 e.printStackTrace();
74             }finally {
75                 try {
76                      if(rs!=null) rs.close(); 
77                      if(pstmt!=null) pstmt.close();
78                      if(con!=null)con.close();
79                 }catch(SQLException e) {
80                     e.printStackTrace();
81                 }catch(Exception e) {
82                     e.printStackTrace();
83                 }
84             }
85         
86 %>
87 </body>
88 </html>

 


程序员灯塔
转载请注明原文链接:Jsp访问数据库
喜欢 (0)