liangoogle

          liangoogle
          隨筆 - 9, 文章 - 0, 評論 - 3, 引用 - 0
          數據加載中……

          jsp-servlet簡單登陸界面 有數據庫連接

          使用mysql數據庫
          使用tomcat服務器
          數據庫連接類:
          package com.servlet;
          import java.sql.*;
          public class conn {
          public static String name;
          public static String mima;
          public conn(String name,String mima){
          conn.name =name;
          conn.mima=mima;
              // 1. 注冊驅動
              try {
                  Class.forName("com.mysql.jdbc.Driver");
              } catch(ClassNotFoundException ex) {
                  ex.printStackTrace();
              }
          }
              public ResultSet date() {
               
                  // 聲明變量,使用,而后關閉
                  Connection conn = null;        //數據庫連接
                  Statement stmt = null;         //數據庫表達式
                  ResultSet rs = null;             //結果集
                  
                  try {
                      //2. 獲取數據庫的連接
                      conn = DriverManager.getConnection
                          ("jdbc:mysql://localhost:3306/dl","root","");
                      
                      //3. 獲取表達式
                      stmt = conn.createStatement();
                      
                      //4. 執行SQL
                      String sql =  "select * from login where user='" + name
                      + "' and pass = '" + mima + "'";
                      rs = stmt.executeQuery(sql);
                      
                      //5. 現實結果集里面的數據
                      //5. 現實結果集里面的數據
                   //   while(rs.next()) {
                       //   System.out.println("id為123的time值=" + rs.getString(1));
                  //    }
                  }
                  catch (Exception ex) {
                      ex.printStackTrace();
                  }
               
                  finally {
                     
                  }
          return rs;
              }
          }
          HttpServlet類:
          package com.servlet;

          import java.io.IOException;
          import java.sql.ResultSet;
          import java.sql.SQLException;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
          import com.sun.xml.internal.bind.v2.runtime.Name;


          /**
           * @author Administrator
           *
           */
          public class login extends HttpServlet {

          /**
          */
          private static final long serialVersionUID = 1L;

          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
          // TODO Auto-generated method stub
          doPost(req, resp);
          }

          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
          // TODO Auto-generated method stub
          String name=req.getParameter("name");
          String mima=req.getParameter("mima");
          conn conn=new conn(name ,mima);
          ResultSet rs=conn.date();
          try {
          if (rs.next())
            {
          resp.sendRedirect("sucessed.jsp?name="+name);
            } else
            //否則登錄失敗
            {
              resp.sendRedirect("index.jsp");
            }
          } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }


          }

          }
          web.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" 
          xmlns="http://java.sun.com/xml/ns/javaee" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
            <welcome-file-list>
              <welcome-file>index.jsp</welcome-file>
            </welcome-file-list>
            <servlet>
          <servlet-name>login</servlet-name>
          <servlet-class>com.servlet.login</servlet-class>
          </servlet>
          <servlet-mapping>
          <servlet-name>login</servlet-name>
          <url-pattern>/jump.jsp</url-pattern>
          </servlet-mapping>
          </web-app>
          index.php:
          <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
          <%
          String path = request.getContextPath();
          String basePath = request.getScheme() + "://"
          + request.getServerName() + ":" + request.getServerPort()
          + path + "/";
          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>
          <base href="<%=basePath%>">

          <title>My JSP 'index.jsp' starting page</title>
          <meta http-equiv="pragma" content="no-cache">
          <meta http-equiv="cache-control" content="no-cache">
          <meta http-equiv="expires" content="0">
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
          <meta http-equiv="description" content="This is my page">
          <!--
          <link rel="stylesheet" type="text/css" href="styles.css">
          -->
          </head>

          <body>

          系 統 登 錄

          <form action="../dl/jump.jsp" method="post">


          用戶名
          <input type="text" name="name">
          <br>

          密&nbsp;&nbsp;&nbsp;&nbsp;碼
          <input type="password" name="mima" />

          <br>
          <input type="submit" value="登錄">

          <input type="reset" value="取消">
          </form>

          </body>
          </html>
          sucessed.jsp
          <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
          <%
          String path = request.getContextPath();
          String basePath = request.getScheme() + "://"
          + request.getServerName() + ":" + request.getServerPort()
          + path + "/";
          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>
          <base href="<%=basePath%>">

          <title>My JSP 'index.jsp' starting page</title>
          <meta http-equiv="pragma" content="no-cache">
          <meta http-equiv="cache-control" content="no-cache">
          <meta http-equiv="expires" content="0">
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
          <meta http-equiv="description" content="This is my page">
          <!--
          <link rel="stylesheet" type="text/css" href="styles.css">
          -->
          </head>

          <body>

          您好:
          <%String name = request.getParameter("name");
          out.print(name);
          %>
          </body>
          </html>

          posted on 2011-04-28 19:58 haojinlian 閱讀(5590) 評論(2)  編輯  收藏

          評論

          # re: jsp-servlet簡單登陸界面 有數據庫連接[未登錄]  回復  更多評論   

          這個怎么破
          2013-11-12 22:47 | jack

          # re: jsp-servlet簡單登陸界面 有數據庫連接  回復  更多評論   

          gjgjcvhgjjbhjkhjkh
          2015-04-21 08:10 | trtu7u

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 安义县| 温州市| 淮阳县| 历史| 独山县| 四川省| 镇宁| 蒙城县| 小金县| 尖扎县| 新巴尔虎右旗| 兰坪| 轮台县| 巴马| 凤庆县| 香港| 都江堰市| 定结县| 邵阳市| 泰兴市| 额济纳旗| 丰镇市| 阿合奇县| 从化市| 呼伦贝尔市| 平安县| 丰都县| 慈溪市| 盐亭县| 休宁县| 凯里市| 崇礼县| 湛江市| 贡嘎县| 浮山县| 云和县| 宜阳县| 万宁市| 祥云县| 金塔县| 延边|