實(shí)現(xiàn)用戶登錄與注冊(cè)_第1頁(yè)
實(shí)現(xiàn)用戶登錄與注冊(cè)_第2頁(yè)
實(shí)現(xiàn)用戶登錄與注冊(cè)_第3頁(yè)
實(shí)現(xiàn)用戶登錄與注冊(cè)_第4頁(yè)
實(shí)現(xiàn)用戶登錄與注冊(cè)_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、數(shù)據(jù)庫(kù)create database student;use student;create table user(id int primary key auto_increment,user varchar(50) not null,pwd varchar(50) not null,name varchar(50) not null,sex varchar(50) not null,age int(50) not null);index.jsp base href= 歡迎來(lái)到學(xué)生管理系統(tǒng) !- 用戶登錄 用戶名: 密碼: 用戶注冊(cè)  addUser.jsp base href= 用戶

2、注冊(cè) !- 用戶注冊(cè) 用戶名: 密碼: 重輸密碼: 姓名: 性別: 男 女 年齡: Dao.javapackage dao;import getConnection.GetConnection;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import com.mysql.jdbc.ResultSet;import bean.User;public class Dao private Connection conn;private PreparedStateme

3、nt pstat;String sql=;/* * 用戶登錄*/public boolean logoin(User user) throws SQLException conn = GetConnection.getConnection(); boolean i = false; sql = select * from user where user=? and pwd=?; pstat = conn.prepareStatement(sql); pstat.setString(1, user.getUser(); pstat.setString(2, user.getPwd(); Resu

4、ltSet rs1 = (ResultSet) pstat.executeQuery(); if (rs1.next() i = true; rs1.close(); pstat.close(); else i = false ; rs1.close(); pstat.close(); conn.close(); return i;/* 用戶注冊(cè)*/public void addUser(User user) conn = GetConnection.getConnection(); sql = insert into user values(?,?,?,?,?); try pstat = c

5、onn.prepareStatement(sql); pstat.setString(1,user.getUser(); pstat.setString(2,user.getPwd(); pstat.setString(3,user.getName(); pstat.setString(4,user.getSex(); pstat.setInt(5,user.getAge(); pstat.executeUpdate(); pstat.close(); conn.close(); catch(SQLException e) e.printStackTrace(); User.javapacka

6、ge bean;public class User private String user;private String pwd;private String name;private String sex;private int age;public String getUser() return user;public void setUser(String user) this.user = user;public String getPwd() return pwd;public void setPwd(String pwd) this.pwd = pwd;public String

7、getName() return name;public void setName(String name) = name;public String getSex() return sex;public void setSex(String sex) this.sex = sex;public int getAge() return age;public void setAge(int age) this.age = age;GetConnection.javapackage getConnection;import java.sql.Connection;import

8、java.sql.DriverManager;import java.sql.SQLException;/* * 用JDBC的方法獲得數(shù)據(jù)庫(kù)的連接*/public class GetConnection /通過(guò)靜態(tài)方法注冊(cè)驅(qū)動(dòng),獲得連接public static Connection getConnection() String driver = com.mysql.jdbc.Driver; String url = jdbc:mysql:/localhost/student; Connection con = null; try Class.forName(driver); try con

9、= DriverManager.getConnection(url,root,); catch (SQLException e) e.printStackTrace(); catch (ClassNotFoundException e) e.printStackTrace(); System.out.println(已獲得數(shù)據(jù)庫(kù)的連接); return con;/*public static void main(String args) getConnection();*/addUserServlet.javapackage servlet;import java.io.IOException

10、;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import dao.Dao;import bean.User;public class addUserServlet extends HttpServlet /* Destruction of the servlet. */public void destroy

11、() super.destroy(); / Just puts destroy string in log / Put your code herepublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(text/html);doPost(request,response);public void doPost(HttpServletRequest request, HttpSe

12、rvletResponse response) throws ServletException, IOException response.setContentType(text/html); response.setCharacterEncoding(utf-8); String user1 = request.getParameter(user); String pwd = request.getParameter(pwd); String name = new String(request.getParameter(name).getBytes(ISO8859_1),utf-8); St

13、ring sex = new String(request.getParameter(sex).getBytes(ISO8859_1),utf-8); String age1 = request.getParameter(age); User user = new User(); user.setUser(user1); user.setPwd(pwd); user.setName(name); user.setSex(sex); int age; if(age1!=null) age = Integer.parseInt(age1); user.setAge(age); Dao dao =

14、new Dao(); dao.addUser(user);/* request.setAttribute(info,new String(添加成功!恭喜! + ); request.setAttribute(id, new String(a); request.setAttribute(denglu,new String(登陸);*/ request.getRequestDispatcher(info.jsp).forward(request,response);public void init() throws ServletException / Put your code herelog

15、inServlet.javapackage servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import bean.User;imp

16、ort dao.Dao;public class loginServlet extends HttpServlet public void destroy() super.destroy(); / Just puts destroy string in log / Put your code herepublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException request.setCharacterEncoding(utf-8)

17、; response.setContentType(text/html); PrintWriter out = response.getWriter(); String name = new String (request.getParameter(user).getBytes(ISO8859_1), GBK); String pwd = new String (request.getParameter(pwd).getBytes(ISO8859_1),UTF-8); User user = new User(); user.setUser(name); user.setPwd(pwd); Dao dao = new Dao(); boolean isLogin; try isLogin = dao.logoin(user); if(isLogin) response.sendRedirect(My

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論