自己改数据库,懂得无需多言,不懂说再多也不懂
https://tomcat.apache.org/
https://mvnrepository.com/
其中的repository目录是我自己建的
mysql
mysql-connector-java
8.0.16
Login
package com.example.loginpro.utils;
import com.example.loginpro.entity.EnterForm;
import java.sql.*;
public class JDBCUtil {
public String url;
public String connect;
public String username;
public String password;
public Connection cn;
public Statement statement;
public ResultSet res;
public JDBCUtil() {
this.url = "com.mysql.cj.jdbc.Driver";
this.connect = "jdbc:mysql://:3306/syf?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true";
this.username = "root";
this.password = "syf20020816";
}
public void connectMysql() throws ClassNotFoundException, SQLException {
Class.forName(this.url);
this.cn = DriverManager.getConnection(this.connect, this.username, this.password);
}
public EnterForm selectOne() throws SQLException {
String sql = "select * from enterform where id = 1";
this.statement = cn.createStatement();
this.res = this.statement.executeQuery(sql);
this.res.next();
EnterForm enterForm = new EnterForm();
enterForm.setUsername(this.res.getString("username"));
enterForm.setPwd(this.res.getString("pwd"));
return enterForm;
}
public void release() throws SQLException {
this.res.close();
this.statement.close();
this.cn.close();
}
}
package com.example.loginpro.service;
import com.example.loginpro.pojo.LoginPojo;
public interface loginService {
// void login(String username,String password);
String login(LoginPojo loginPojo);
}
package com.example.loginpro.service.impl;
import com.example.loginpro.entity.EnterForm;
import com.example.loginpro.mapper.impl.LoginMapperImpl;
import com.example.loginpro.pojo.LoginPojo;
import com.example.loginpro.service.loginService;
public class LoginServiceImpl implements loginService {
@Override
public String login(LoginPojo loginPojo) {
String answer = "登录失败";
//获取数据库中的用户名和密码与loginpojo进行对比
LoginMapperImpl loginMapper = new LoginMapperImpl();
EnterForm dataFormSQL = loginMapper.getDataFormSQL();
if(!loginPojo.getUsername().equals(dataFormSQL.getUsername())){
return answer = "用户名错误";
}else if(!loginPojo.getPassword().equals(dataFormSQL.getPwd())) {
return answer = "密码错误";
}else{
answer = "登录成功";
return answer;
}
}
}
package com.example.loginpro.pojo;
public class LoginPojo {
private String username;
private String password;
public LoginPojo(String username, String password) {
this.username = username;
this.password = password;
}
public LoginPojo() {
}
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;
}
@Override
public String toString() {
return "LoginPojo{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.example.loginpro.pojo;
public class LoginPojo {
private String username;
private String password;
public LoginPojo(String username, String password) {
this.username = username;
this.password = password;
}
public LoginPojo() {
}
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;
}
@Override
public String toString() {
return "LoginPojo{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.example.loginpro.mapper.impl;
import com.example.loginpro.entity.EnterForm;
import com.example.loginpro.mapper.LoginMapper;
import com.example.loginpro.utils.JDBCUtil;
import java.sql.SQLException;
public class LoginMapperImpl implements LoginMapper {
@Override
public EnterForm getDataFormSQL() {
EnterForm enterForm = new EnterForm();
//连接数据库
JDBCUtil jdbcUtil = new JDBCUtil();
try {
jdbcUtil.connectMysql();
//进行搜索
enterForm = jdbcUtil.selectOne();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
jdbcUtil.release();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return enterForm;
}
}
package com.example.loginpro.entity;
public class EnterForm {
private Long id;
private String username;
private String pwd;
public EnterForm(Long id, String username, String pwd) {
this.id = id;
this.username = username;
this.pwd = pwd;
}
public EnterForm() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "EnterForm{" +
"id=" + id +
", username='" + username + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
最后启动你的Tomcat访问localhost:8080/login.html