zoukankan      html  css  js  c++  java
  • JDBC-oracle(登陆)

    题目:

    第一步:创建用户表,并插入数据(插入后记得commit

    create table users
    (
    name varchar2(16),
    password varchar2(16)
    );
    insert into users values('lisi','123');
    insert into users values('zhangsan','123');

    第二步:编写登陆界面(index.jsp)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <html>
      <head>    
        <title>index.jsp</title>
      </head>  
      <body>
        <form action="dologin.jsp">
            用户名:<input name="user" type="text"/><br>
            密码:<input name="password" type="text" /><br>
            <input type="submit" value="lojin" />
        </form>
      </body>
    </html>

    第三步:编写工具类(题目要求将COnnection的获取封装,我这里还封装了关闭连接的部分)(Util.jsva)

    关闭注意事项:

    1,关闭先判断(如果不为空在判断,防止未打开就关闭的空指针异常)

    2,try{}catch(){}捕获异常

    3,关闭后及时垃圾回收,xx=null;

    package songyan.Util;
    
    import java.sql.*;
    
    public class Util {
    
        static Connection conn;
        static String sql="select * from users";
     
     
         static String driver= "oracle.jdbc.driver.OracleDriver";
        static String user= "scott";
        static String url="jdbc:oracle:thin:@localhost:1521:inspur";
        static String pass= "tiger";
        
        public static Connection getConnection() throws Exception
        {
            //加载驱动
             Class.forName(driver);
             //获取连接
             return DriverManager.getConnection(url,user,pass);
        }
        
        public static void closeAll(ResultSet rs,PreparedStatement sta,Connection conn) {
            //关闭连接
             if(rs!=null)
             {
                 try{rs.close();}catch(Exception e){}
                 rs=null;
             }
             if(sta!=null)
             {
                 try{rs.close();}catch(Exception e){}
                 rs=null;
             }
             if(conn!=null)
             {
                 try{rs.close();}catch(Exception e){}
                 rs=null;
             }
        }
    }

    第四步:编写处理页面(dologin.jsp)

    注意:

    1,导包(在开始就导入oracle驱动包,放在webinf中的lib目录下)

    2,引入包,songyan.Util.*:自己写的工具

      java.sql.*:java提供的操作数据库的包

    <%@ page language="java"  pageEncoding="UTF-8"%>
    <%@ page import="java.util.*,songyan.Util.*,java.sql.*" %>
    <html>
      <head>    
        <title>dolojin.jsp</title>
      </head>  
      <body>
        <%!
         Connection conn;
         PreparedStatement sta;
         ResultSet rs;
         String sql="select * from users";
     
          String driver= "oracle.jdbc.driver.OracleDriver";
         String user= "scott";
         String url="jdbc:oracle:thin:@localhost:1521:inspur";
         String pass= "tiger";
         String name="";
         String password="";
         %>
         <%
          try {
          name=request.getParameter("user");
          password=request.getParameter("password");
             conn=Util.getConnection();
             //获取sta对象
             sql="select * from users where name=? and password=?";
             sta=conn.prepareStatement(sql);
             sta.setString(1, name);
             sta.setString(2, password);
             
             //执行sql获取结果
             rs=sta.executeQuery();
             //处理结果
             if(rs.next())
             {
                 request.getRequestDispatcher("success.jsp").forward(request, response);
             }else{
                 response.sendRedirect("index.jsp");
             }
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
         finally{
             Util.closeAll(rs, sta, conn);
         }
          %>
      </body>
    </html>

    第五步:编写成功登陆界面

    <%@ page language="java"  pageEncoding="UTF-8"%>
    <html>
      <head>    
        <title>success</title>
      </head>  
      <body>
    <%
    out.print("登录成功");
     %>
      </body>
    </html>

    1.在整个过程中,修改.java文件或者修改WEB-INF中的内容都需要重启tomcat(也可以设置自动重启)

    2.在这个题目中涉及到两个username,password,一个是你登陆oracle的用户和密码,一个是自己写的登陆窗口的用户名和密码,在命名上一定要注意区分

    不足之处欢迎补充*&*

  • 相关阅读:
    CS224n, lec 10, NMT & Seq2Seq Attn
    CS231n笔记 Lecture 11, Detection and Segmentation
    CS231n笔记 Lecture 10, Recurrent Neural Networks
    CS231n笔记 Lecture 9, CNN Architectures
    CS231n笔记 Lecture 8, Deep Learning Software
    CS231n笔记 Lecture 7, Training Neural Networks, Part 2
    pytorch坑点排雷
    Sorry, Ubuntu 17.10 has experienced an internal error
    VSCode配置python插件
    tmux配置与使用
  • 原文地址:https://www.cnblogs.com/excellencesy/p/8587244.html
Copyright © 2011-2022 走看看