众所周知,我们在使用JAVA开发的时候,用户的数据都是存放在数据库当中的,可是市面上有那么多种类的数据库,为了统一各个数据库和java的连接规范,就出现了JDBC。
JDBC为访问不同数据库提供了统一的接口,java程序员使用JDBC,可以连接任何提供JDBC驱动程序的数据库系统,从而完成对数据库的各种操作。
String url ="jdbc:mysql://localhost:3306/abc"; //abc为数据库名称
String user = "root"; //定义字符串变量存入mysql登录名
String password = "123456"; //定义字符串存入mysql登录密码
Class.forName("com.mysql.jdbc.Driver"); //加载mysql驱动
Connection connection = DriverManager.getConnection(url,user,password);
Statement statement = connection.createStatement(); //建立连接
if (conn!=null){
out.print("连接成功"+"
");
}else{
out.print("连接失败"+"
");
}
String s1=request.getParameter("username");
String s2=request.getParameter("password");
request.setAttribute("s1", s1);
//判断用户名、密码是否为空
if(s1 == "" || s1.length() == 0){
request.setAttribute("namemsg","用户名为空!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
if(s2== "" || s2.length() == 0){
request.setAttribute("pwdmsg","密码为空!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
String sql = String.format("SELECT * FROM login WHERE `username` = '%s' AND `password` = '%s'",s1,s2);
ResultSet resultSet = statement.executeQuery(sql);//返回的结果集
if (resultSet.next()){
request.getRequestDispatcher("login.jsp").forward(request,response); //匹配成功进入登录成功页面
}else if(s1.equals("") || s2.equals("")){
request.getRequestDispatcher("loginf.jsp").forward(request,response); //用户名和密码有一个村务
}else{
request.getRequestDispatcher("loginf.jsp").forward(request,response); //用户和密码全部错误
}
package servlet;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author s
* @version 1.0
*/
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public loginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s1=request.getParameter("username");
String s2=request.getParameter("password");
request.setAttribute("s1", s1);
//判断用户名、密码是否为空
if(s1 == "" || s1.length() == 0){
request.setAttribute("namemsg","用户名为空!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
if(s2== "" || s2.length() == 0){
request.setAttribute("pwdmsg","密码为空!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
//密码部分结束
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
//加载mysql驱动
String url ="jdbc:mysql://localhost:3306/abc";
String user = "root";
String password = "123456";
try {
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
Connection connection = DriverManager.getConnection(url,user,password);
Statement statement = connection.createStatement();
String sql = String.format("SELECT * FROM login WHERE `username` = '%s' AND `password` = '%s'",s1,s2);
ResultSet resultSet = statement.executeQuery(sql);//返回的结果集
if (resultSet.next()){
request.getRequestDispatcher("login.jsp").forward(request,response);
}else if(s1.equals("") || s2.equals("")){
request.getRequestDispatcher("loginf.jsp").forward(request,response);
}
else{request.getRequestDispatcher("loginf.jsp").forward(request,response);}
}catch(ClassNotFoundException | SQLException e) {
e.printStackTrace();
System.out.println("失败");
}
}
}
<%--
Created by IntelliJ IDEA.
User: 86151
Date: 2022/5/24
Time: 14:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
用户登录
<%----%>
<%-- 管理员登录--%>
<%--
--%>