登录验证前端、后端加数据库入门代码
Servlet代码
package SanWa.Servlet1;
import SanWa.Servlet2.JDBCUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@WebServlet(name = "ServletLogin", value = "/bow")
public class ServletLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection connection = JDBCUtils.getConnection();
String sql="select * from jiaoyan where username='"+username+"' and password='"+password+"'";
PreparedStatement preparedStatement2 = null;
try {
preparedStatement2 = connection.prepareStatement(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
boolean i = false;
try {
i = preparedStatement2.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (i==true) {
System.out.println("登陆成功");
}else {
System.out.println("登陆失败");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
工具代码
package SanWa.Servlet2;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String url;
private static String username;
private static String password;
static {
/*Properties p = new Properties();
try {
p.load(new FileInputStream("src\\SanWa\\Servlet2\\info.properties"));
} catch (IOException e) {
e.printStackTrace();
}
try {
Class.forName("p.getProperty("path")");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
url = p.getProperty("url");
username = p.getProperty("username");
password = p.getProperty("password");*/
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
url="jdbc:mysql://localhost:3309/loginxx";
username="root";
password="131427";
}
// 把获取连接对和释放资源的操作 抽取到工具类
private JDBCUtils() {
}
// 获取连接对象
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
//释放资源
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void close(Connection connection, Statement statement) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
MySQL代码
/*
SQLyog Ultimate v12.08 (64 bit)
MySQL - 5.5.27 : Database - loginxx
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`loginxx` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `loginxx`;
/*Table structure for table `jiaoyan` */
DROP TABLE IF EXISTS `jiaoyan`;
CREATE TABLE `jiaoyan` (
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `jiaoyan` */
insert into `jiaoyan`(`username`,`password`) values ('xxxx','123456');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
前端代码
<%--
Created by IntelliJ IDEA.
User: yllch
Date: 2020/10/16
Time: 9:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="http://localhost:8080/Web1_war_exploded/bow" method="post" >
用户名 <input type="text" name="username" >
密码 <input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
运行结果
已连接到服务器
[2020-10-16 05:24:33,594] Artifact Web1:war exploded: 正在部署工件,请稍候…
16-Oct-2020 17:24:34.865 淇℃伅 [RMI TCP Connection(5)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars 鑷冲皯鏈変竴涓狫AR琚壂鎻忕敤浜嶵LD浣嗗皻鏈寘鍚玊LD銆� 涓烘璁板綍鍣ㄥ惎鐢ㄨ皟璇曟棩蹇楄褰曪紝浠ヨ幏鍙栧凡鎵弿浣嗘湭鍦ㄥ叾涓壘鍒癟LD鐨勫畬鏁碕AR鍒楄〃銆� 鍦ㄦ壂鎻忔湡闂磋烦杩囦笉闇�瑕佺殑JAR鍙互缂╃煭鍚姩鏃堕棿鍜孞SP缂栬瘧鏃堕棿銆�
16-Oct-2020 17:24:36.357 璀﹀憡 [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [1,372] milliseconds.
[2020-10-16 05:24:36,405] Artifact Web1:war exploded: 工件已成功部署
[2020-10-16 05:24:36,405] Artifact Web1:war exploded: Deploy took 2,811 milliseconds
16-Oct-2020 17:24:43.519 淇℃伅 [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployDirectory 閮ㄧ讲 web 搴旂敤绋嬪簭鐩綍 [D:\tomcat\apache-tomcat-9.0.14\webapps\manager]
16-Oct-2020 17:24:43.599 淇℃伅 [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [D:\tomcat\apache-tomcat-9.0.14\webapps\manager] has finished in [80] ms
登陆成功
登陆成功
登陆成功