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

jsp简易登录和注册及JDBC连接Oracle

时间:2018-07-02 10:56:56      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:forward   问题   编码   catch   xml文件   ref   web.xml   page   www.   

 

 一:JDBC连接数据库(Oracle参考)

public class DBTest {//测试
 public static void main(String[] args) {
  ResultSet rs = null;
  Statement stmt = null;
  Connection conn = null;
  try {
   // 加载驱动
   Class.forName("oracle.jdbc.driver.OracleDriver");
   // 连接数据库
   String url="jdbc:oracle:thin:@192.168.0.xxx:1521:orcl";
   conn = DriverManager.getConnection(url, "ms_test", "1");
   System.out.println("连接成功...");
   stmt = conn.createStatement();
   rs = stmt.executeQuery("select * from test");
   while (rs.next()) {
    System.out.println(rs.getString("uname"));// 列名
    System.out.println(rs.getString("pwd"));// 列名
   
   }
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    if (rs != null) {
     rs.close();
     rs = null;
    }
    if (stmt != null) {
     stmt.close();
     stmt = null;
    }
    if (conn != null) {
     conn.close();
     conn = null;
    }
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
}
如能获取则成功!
注:ojbc6.jar的问题,特别注意应当放在lib目录下。

二:JSP简易登录界面
1:创建动态的web项目
2:新建JSP文件(login.jsp)
3:修改web.xml文件
<!-对应登录JSP   ->
<welcome-file-list>
  <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>
4:跳转页面
1)成功success.jsp
2)失败fail.jsp
5:处理事务界面
chuli.jsp
如下:
login.jsp  编码方式为:UTF-8或者GBK
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
  <form action="chuli.jsp" method="post">//这里跳转处理事务界面,再判断跳转成功或者失败界面。
     <table align="center">
        <tr>
           <td>账号</td>
           <td><input type="text" name="uname"></td>
        </tr>
        <tr>
           <td>密码</td>
           <td><input type="password" name="pwd"></td>
        </tr>
        <tr>
           <td><input type="submit" value="登录"></td>
           <td><input type="reset" value="重置"></td>
        </tr>
     </table>
  </form>
</body>
</html>

success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTM1 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
登录成功
</body>
</html>

fail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
登录失败
</body>
</html>

核心处理界面
chuli.jsp
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<!-- 获取登录信息 -->
<% String uname=request.getParameter("uname"); 
   String pwd=request.getParameter("pwd"); %>
<%
   try{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    String url="jdbc:oracle:thin:@192.168.0.248:1521:orcl";
    Connection conn = DriverManager.getConnection(url, "ms_test", "1");
    String sql="select * from test where uname=?and pwd=?";
    PreparedStatement ps=conn.prepareStatement(sql);
    ps.setString(1, uname);
    ps.setString(2, pwd);
    ResultSet re=ps.executeQuery();
    if(re.next()){
     %>
     <jsp:forward page="success.jsp"></jsp:forward>
     <% 
    }else{
     %>
     <jsp:forward page="fail.jsp"></jsp:forward>
     <% 
    }
   }catch(Exception e){
    e.printStackTrace();
   }
%> 
</body>
</html>
总结:驱动包存放目录问题,驱动包可以在Oracle安装目录下查找 action=“”处理问题,编码问题。
 
三:JSP简易注册
需要3个jsp文件
1.注册zhuce.jsp
2.注册成功success1.jsp
3.注册失败fail1.jsp
4.事务处理核心chuli2.jsp
如下:
注册zhuce.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
 <form action="chuli2.jsp" method="post">//核心处理事务jsp
  <table align="center">
   <tr>
    <td>账号:</td>
    <td><input type="text" name="uname"></td>
   </tr>
   <tr>
    <td>密码:</td>
    <td><input type="password" name="pwd"></td>
   </tr>
   <tr>
    <td><input type="submit" value="注册"></td>
    <td><input type="reset" value="重置"></td>
    <td><a href="login.jsp">返回登录</a></td>
   </tr>
  </table>
 </form>
</body>
</html>

chuli2.jsp
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<!-- 获取注册信息 -->
<% String uname=request.getParameter("uname"); 
   String pwd=request.getParameter("pwd"); %>
<%
   try{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    String url="jdbc:oracle:thin:@192.168.0.248:1521:orcl";
    Connection conn = DriverManager.getConnection(url, "ms_test", "1");
    String sql="insert into test values (?,?)";//insert into 信息插入数据库
    PreparedStatement ps=conn.prepareStatement(sql);
    ps.setString(1, uname);
    ps.setString(2, pwd);
    if(ps.executeUpdate()>-1){//对应的为update
     %>
     <jsp:forward page="success1.jsp"></jsp:forward>
     <% 
    }else{
     %>
     <jsp:forward page="fail1.jsp"></jsp:forward>
     <% 
    }
   }catch(Exception e){
    e.printStackTrace();
   }
%> 
</body>
</html>

success1.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTM1 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
注册成功<a href="login.jsp">登录</a>//注册成功则点击登录跳转登录页面
</body>
</html>

fail1.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
注册失败<a href="zhuce.jsp">再次注册</a>
</body>
</html>

jsp简易登录和注册及JDBC连接Oracle

标签:forward   问题   编码   catch   xml文件   ref   web.xml   page   www.   

原文地址:https://www.cnblogs.com/chenxiaoxu/p/9252194.html

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