jsp簡易登錄和注冊及JDBC連接Oracle


 

 一: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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM