mysql表
create table abc( no int primary key auto_increment, name varchar(20), pwd varchar(20) );
login.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="doLogin.jsp" method = "post"> 登录名:<input type = "text" name = "name"/><br/> 密码:<input type = "password" name = "pwd"/><br/> <input type = "submit" value = "注册"/><br/> </form> </body> </html>
weclome.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>
doLogin.jsp
<%@page import="com.student.dao.LoginDao"%>
<%@page import="com.school.work.Login"%>
<%@ 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></title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
Login login = new Login();
String uname = request.getParameter("name");
String upwd = request.getParameter("pwd");
login.setName(uname);
login.setPwd(upwd);
LoginDao dao = new LoginDao();
if (dao.register(login) > 0) {
response.sendRedirect("welcome.jsp");
}
%>
</body>
</html>
LoginDao.java
package com.student.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.school.work.Login; public class LoginDao { private static final String URL = "jdbc:mysql://localhost:3306/jsp"; private static final String NAME = "root"; private static final String PASSWORD = "root"; private static java.sql.PreparedStatement pstmt = null; private static Connection connection = null; // 注册 public int register(Login login) { int count = 0; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(URL, NAME, PASSWORD); String sql = "insert into abc(name,pwd) values(?,?)"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, login.getName()); pstmt.setString(2, login.getPwd()); count = pstmt.executeUpdate(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } return count; } }
Login.java
package com.school.work; public class Login { private int id; private String name; private String pwd; public Login() { } public Login(String name, String pwd) { this.name = name; this.pwd = pwd; } public Login(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }