Struts2 +JDBC——简单登录

文件结构:Struts2 +JDBC——简单登录_第1张图片Struts2 +JDBC——简单登录_第2张图片Struts2 +JDBC——简单登录_第3张图片



数据库MySQL:


一个struts2需要在Myeclipse中建立一个Web Project。勾选web.xml


1.在lib目录中导入struts jar包(连接数据库时别忘导mysql jar包)


2.在web.xml中加入struts默认配置:


    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
    struts2
    /*
  

3建立pojo实体模型

package com.ibm.pojo;

public class Person {
	private String name;
	private String password;
	private String email;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}

4.建立register.jsp,login.jsp,error.jsp

register.jsp

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



  
    
    
    My JSP 'register.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
  
    
    
    
    
    
  

login.jsp

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




  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
    
    
    
    
    
    
">注册
   
   
 

 error.jsp 
  

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



  
    
    
    My JSP 'error.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    error!
  

success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'success.jsp' starting page
    
	
	
	    
	
	
	

  
  

Welcome

5.完成继承ActionSupport的action

LoginAction.java

package com.ibm.action;

import java.sql.ResultSet;

import com.ibm.pojo.Person;
import com.ibm.util.DButil;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	private Person persona = new Person();

	public Person getPersona() {
		return persona;
	}

	public void setPersona(Person persona) {
		this.persona = persona;
	}

	@Override
	public String execute() throws Exception {
		DButil dButil = new DButil();
		ResultSet rs = null;
		String sql = "select * from person p where p.name=? and p.password=?";
		rs = dButil.executeQuery(sql, persona.getName(),persona.getPassword());
		if(rs.next()){
			dButil.close();
			return SUCCESS;
		}else{
			dButil.close();
			return ERROR;
		}
	}
}

RegisterAction.java

package com.ibm.action;

import com.ibm.pojo.Person;
import com.ibm.util.DButil;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport{
	private Person persona = new Person();

	public Person getPersona() {
		return persona;
	}

	public void setPersona(Person persona) {
		this.persona = persona;
	}

	@Override
	public String execute() throws Exception {
		DButil dbutil = new DButil();
		String sql="insert into person(name,password,email) values(?,?,?)";
		int executeUpdate = dbutil.executeUpdate(sql,persona.getName(),persona.getPassword(),persona.getEmail());
		if(executeUpdate==1){
			dbutil.close();
			return SUCCESS;
		}else {
			dbutil.close();
			return ERROR;
		}
	}
}

6.在src目录下新建struts.xml配置action




	
		
		/login.jsp
		/error.jsp
		
		
		/success.jsp
		/error.jsp
		
	
7.Dbutil数据库工具类

package com.ibm.util;

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


public class DButil {
	private final String DBURL ="jdbc:mysql://localhost:3306/ibm";
	private final String DBUSER = "root";
	private final String DBPASSWORD = "root";
	private Connection con = null;
	private PreparedStatement stmt = null;
	private ResultSet rs = null;
	
	public DButil(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void close(){
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public ResultSet executeQuery(String sql,Object... params) {//可变参数
		try {
			stmt = con.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				this.stmt.setObject(i+1, params[i]);
			}
			rs = stmt.executeQuery();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		return rs;
	}
	public int executeUpdate(String sql,Object... params){
		int result = 0;
		try {
			stmt = con.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				this.stmt.setObject(i+1, params[i]);
			}
			result = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}

                                




你可能感兴趣的:(Struts2 +JDBC——简单登录)