ora login

分类: 系统运维
1 数据库:
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,
*** varchar(50) not null,
age int(50) not null
);

2 index.jsp

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






欢迎来到学生管理系统









用户登录















用户名:
密 码:

 





3 addUser.jsp

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






用户注册











用户注册
































用户名:
密码:
重输密码:
姓名:
性别:
年龄:





4 DAO.java 实现与数据库的连接用户名验证与用户名新建添加

package 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 PreparedStatement 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());

ResultSet rs1 = (ResultSet) pstat.executeQuery();
if (rs1.next())
{
i = true;
rs1.close();
pstat.close();
}
else
{
i = false ;
rs1.close();
pstat.close();
}
conn.close();
return i;
}
/**
* 用户注册
*/
public void addUser(User user){
conn = GetConnection.getConnection();
sql = "insert into user values(?,?,?,?,?)";
try{
pstat = conn.prepareStatement(sql);
pstat.setString(1,user.getUser());
pstat.setString(2,user.getPwd());
pstat.setString(3,user.getName());
pstat.setString(4,user.get***());
pstat.setInt(5,user.getAge());
pstat.executeUpdate();
pstat.close();
conn.close();

}catch(SQLException e){
e.printStackTrace();
}

}
}
5 User.java

package bean;

public class User {
private String user;
private String pwd;
private String name;
private String ***;
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String get***() {
return ***;
}
public void set***(String ***) {
this.*** = ***;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

6 GetConnection.java 实现与数据库之间的连接

package getConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
*
* 用JDBC的方法获得数据库的连接
*
*/
public class GetConnection {
//通过静态方法注册驱动,获得连接

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 = DriverManager.getConnection(url,"root","");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("已获得数据库的连接");
return con;
}
/*public static void main(String args[]){
getConnection();
}*/
}
7 addUserServlet.java

package servlet;

import java.io.IOException;

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() {
super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse 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");
String *** = new String(request.getParameter("***").getBytes("ISO8859_1"),"utf-8");
String age1 = request.getParameter("age");
User user = new User();
user.setUser(user1);
user.setPwd(pwd);
user.setName(name);
user.set***(***);
int age;
if(age1!=null)
{
age = Integer.parseInt(age1);
user.setAge(age);
}
Dao dao = 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 here

}

}


8 loginServlet.java

package 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;
import dao.Dao;

public class loginServlet extends HttpServlet {
public void destroy() {
super.destroy(); // Just puts "destroy" string in log

// Put your code here

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
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("MyJsp.jsp");
}else{
response.sendRedirect("index.jsp");
}

} catch (SQLException e)
{
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void init() throws ServletException {
}

}

你可能感兴趣的:(basic,数据库,java,运维)