近期我打算使用 java web 完成一个管理系统用于网博杯比赛使用

public CarCard checkUser(String username,String password){
            try{
                pstmt = ct.prepareStatement("select * from [CarCard] where CardID=? and CardPwd=?");
                        pstmt.setString(1,username);
                        pstmt.setString(2,password);
                        ResultSet rs = pstmt.executeQuery();
                        CarCard user = new CarCard();
                        while(rs.next()){
                        user.setCardID(rs.getString(1));
                        user.setCardPwd(rs.getString(2));
                        //....
                        return user;
                        }
                        return null;
            }catch(Exception e){
                e.printStackTrace();
                return null;
            }
        }

以上使用java连接数据库的时候使用这种方式可以提高效率

try {
			pstmt.setString(1, username);
			pstmt.setString(2, password);//相当于模式匹配
			ResultSet rs = pstmt.executeQuery();//在上面的模式匹配下。完成查询。提高效率
			if(rs.next()) {//说明查到了信息
				isExists = true;
			}
		} catch (SQLException e) {
		
			e.printStackTrace();
		}finally {
			MysqlDeal.getmysqlDeal().closeDBResources();
		}

在前后端交互的时候可以使用重定向将java的后端数据在传给前端

//登录成功重定向到index.jsp
			response.sendRedirect("index.jsp");

对于后端当在web.xml中使用servlet完成配置之后就可以通过request.getParameter()获取后端的数据

String username =request.getParameter("username");
		String password =request.getParameter("password");

在web.xml中的额配置如下

<servlet>
  	<servlet-name>LoginServlet</servlet-name>
  	<servlet-class>com.byc.login.Login</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>LoginServlet</servlet-name>
  	<url-pattern>/login</url-pattern>
  </servlet-mapping>

在项目中 的com.byc.login包中有四个类

package com.byc.login;
import java.io.IOException;
import java.io.PrintWriter;

import javax.persistence.PrimaryKeyJoinColumn;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet implements Servlet {//本部分用来接收前端发来的信息然后进行处理
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//getParameter获取前端的信息
		String username =request.getParameter("username");
		String password =request.getParameter("password");
		LoginInfo loginInfo =new LoginInfo(username,password);//通过构造函数将信息传出
		//下面需要判断消息是否可以登录
		LoginServlet loginServlet=new LoginServlet();
		boolean legal =loginServlet.legal(loginInfo);//将信息移交审查并返回结果
		//根据判断消息的对错做出不同的反应
		if(legal)
		{
			//登录成功重定向到index.jsp
			response.sendRedirect("index.jsp");
		}
		else {
			//重定向到error.jsp
			//response.sendRedirect("error.jsp");
			//转发请求
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
		
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse respone)
			throws ServletException, IOException {
		doGet(request, respone);
	}
	
}

 

package com.byc.login;

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

import com.byc.mysql.MysqlDeal;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class LoginDao {
	
	public boolean isExists(String username, String password) {
		//从数据库中提取信息查看登录的用户是否在数据库中
		boolean isExists = false;//初始化值设置为false
		String sql = "select * from login where username=? and password=?";
		//执行检查
		PreparedStatement pstmt=MysqlDeal.getmysqlDeal().getPreparedStatement(sql);
		try {
			pstmt.setString(1, username);
			pstmt.setString(2, password);//相当于模式匹配
			ResultSet rs = pstmt.executeQuery();//在上面的模式匹配下。完成查询。提高效率
			if(rs.next()) {//说明查到了信息
				isExists = true;
			}
		} catch (SQLException e) {
		
			e.printStackTrace();
		}finally {
			MysqlDeal.getmysqlDeal().closeDBResources();
		}
		return isExists;
	}
}

 

package com.byc.login;

public class LoginInfo {
	private String username;
	private String password;
	public  LoginInfo(String username, String password) {//构造函数创建用户的信息
		this.username = username;
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

package com.byc.login;


public class LoginServlet {
	LoginDao logindao=new LoginDao();
	public boolean legal(LoginInfo loginInfo) {//对LoginDao出来的信息进行审查
		return logindao.isExists(loginInfo.getUsername(),loginInfo.getPassword());
	}
}

对于数据库的处理如下

package com.byc.mysql;
/*
 * 对于数据库连接处理的模板在需要的时候可以直接拿来使用
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.jsp.tagext.TryCatchFinally;

import com.sun.servicetag.Installer;

//数据库的连接管理
public class MysqlDeal {
	static MysqlDeal mysqlDeal=new MysqlDeal();
	private Connection conn;//声明连接的对象。连接之后获得连接的句柄
	private Statement stmt;//创建statement类对象,用来执行SQL语句!
	private PreparedStatement pstmt;//实例化SQL执行句柄pstmt
	
	public static MysqlDeal getmysqlDeal()
	{
		return mysqlDeal;
	}
	public MysqlDeal() {
		
	}
	static {
		//加载驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	//实例化数据库连接conn
	public Connection getConnection() {
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mylearn", "root", "root");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	//实例化SQL执行句柄stmt
	public Statement getStatement() {
		Connection conn = getConnection();//获得连接的对象
		try {
			if(conn != null) {
				stmt = conn.createStatement();//如果连接成功
				//创建statement类对象,用来执行SQL语句
				//ResultSet rs = stmt.executeQuery(sql);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stmt;
	}
	
	//实例化SQL执行句柄pstmt
	public PreparedStatement getPreparedStatement(String sql) {
		Connection conn = getConnection();
		try {
			if(conn != null) {
				pstmt = conn.prepareStatement(sql);
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return pstmt;
	}
	
	//关闭数据库连接资源
	public void closeDBResources() {
		try {
			if(pstmt != null && !pstmt.isClosed()) {
				pstmt.close();
			}
			if(stmt != null && !stmt.isClosed()) {//如果stmt不为空,并且还未关闭
				stmt.close();
			}
			if(conn != null && !conn.isClosed()) {
				conn.close();
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		
	}
}

至此关于java作为后台的简单登录界面就已经完成了