1 建立一个连接Mysql的工具类 JdbcUtil.JAVA
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
private static String Driver="org.gjt.mm.mysql.Driver";
private static String Url="jdbc:mysql://localhost:3306/db_mysqldatabases20140509";//后跟数据库名字
private static String USER="root";
private static String PASSWORD="123456";
private static Connection conn=null;
private static Statement stmt=null;
public static Connection getConnection(){
try {
Class.forName(Driver).newInstance();
conn=DriverManager.getConnection(Url,USER,PASSWORD);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(){
try {
stmt=getConnection().createStatement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stmt;
}
public void closeConn(){
if(JdbcUtil.conn!=null || JdbcUtil.stmt!=null){
try {
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2 UserInfo.java
package cn.xh.login.bean;
public class UserInfo {
private String name; //用户名
private String pwd ; // 密码
private String sex; //性别
private int age; //年龄
private String email; //Email
public UserInfo(){}
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;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
3 建立一个servlet LoginServlet.java
package cn.xh.login.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import cn.xh.login.bean.UserInfo;
import cn.xh.login.util.JdbcUtil;
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 获取
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
Connection conn = null;
Statement st = null;
ResultSet re = null;
try {
conn = JdbcUtil.getConnection();
st = JdbcUtil.getStatement();
String sql = "select * from db_user where name='" + name
+ "' and pwd='" + pwd + "'";
re = st.executeQuery(sql);
// 登录验证
if (re.next()) {
UserInfo user = new UserInfo();
user.setName(name);
user.setPwd(pwd);
session.setAttribute("user", user);
out.println("<font algin='center' color='red' size='5'>登录成功 3秒后自动跳转</font>");
response.setHeader("refresh", "1;url = '/Login/Filter/index1.jsp'");
} else {
response.sendRedirect("/Login/Login.jsp");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4 web.XML 配置
5 在工程 web-inf 下导入 数据库驱动
mysql-connector-java-5.0.8-bin