一、建表
create batabase test;
create table user(
id int,
name varchar(20),
pwd varchar(10)
);
insert into user (id,name,pwd) values(1,'张三','123123'),
(2,'李四','123456'),(3,'艾鹏','121212');
二、导包
下载MySQL jar包, 可以上maven:https://mvnrepository.com/artifact/mysql/mysql-connector-java
注意与MySQL的版本一致
我的版本是8.0.22所以下载这个包
点击即可下载
下载之后复制到lib目录下面
o k !!!!!!!!!!!
三、加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
- 这里驱动一般是com.mysql.jdbc.Driver
- 由于我的JAVA EE IDE 上面报了一个错误说版本原因让我使用这个包,所以根据情况更换
四、创建链接
String url = "jdbc:mysql://localhost:3306/test?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String username = "root";
String password = "Aa123456";
Connection conn = DriverManager.getConnection(url,username,password);
conn.createStatement();
- url 表示要连接的数据库
- username 数据库登录用户
- password 对应用户的登录密码
- 第四行连接数据库
- 第五行创建一个数据库连接 链接对象是Statement对象
- 这里面会抛出异常
实验七
7.1编写工具类
MysqlUtils.java 写的是一个工具类,用于生产数据库连接
package com.qzw.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.qzw.pojo.User;
public class MysqlUtils {
public Statement getStatement(){
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String username = "root";
String password = "Aa123456";
//创建连接
Connection conn = DriverManager.getConnection(url,username,password);
return conn.createStatement();
}catch (Exception e) {
// TODO: handle exception
System.out.println("Error1");
return null;
}
}
}
- 这里url里面的test是我的数据库名字 密码,账号根据自己的设置填写
7.2 实体类
创建实体类, 根据数据库里面的数据类型创建实体类
package com.qzw.pojo;
public class User {
private int id;
private String name;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
- 实体类只是数据类型 方法通过接口去实现
7.3 接口
package com.qzw.dao;
import java.util.List;
import com.qzw.pojo.*;
public interface UserDao {
//获取用户列表
List<User> getUser();
//判断登录信息是否正确
boolean ifLogin(String name, String pwd);
}
- 定义接口,由于只需要显示信息定义一个接口就可以
下面来实现接口
package com.qzw.dao;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.qzw.pojo.*;
import com.qzw.utils.MysqlUtils;
public class UserDao1 implements UserDao {
public List<User> getUser(){
Statement str = null;
List<User> list = new ArrayList<User>();
//创建链表用来存贮查询返回的信息
try {
str = new MysqlUtils().getStatement();
//使用工具类创建链接
String sql = "select id,name,pwd from user";
//编写sql语句
ResultSet re = str.executeQuery(sql);
//执行sql语句 结果在ResultSet结果集里面
while(re.next()) {
int id = re.getInt("id");
String name = re.getString("name");
String pwd = re.getString("pwd");
User user = new User(id,name,pwd);
list.add(user);
//从结果集取出信息构造User数据类型装入链表里面
}
str.close(); //关闭连接
re.close(); //关闭结果集
} catch (Exception e) {
// TODO: handle exception
System.out.println("Error");
}
return list;
}
public boolean ifLogin(String name, String pwd) {
Statement str = null;
try {
str = new MysqlUtils().getStatement();
String sql = "select * from user where name = '" + name +"' and pwd = '" + pwd +"'";
//String kString = "select * from user where name = '" + name +"' and pwd = '" + pwd +"'";
// System.out.println(sql);
ResultSet re = str.executeQuery(sql);
if(re.next()) //判空
return true;
else
return false;
} catch (Exception e) {
// TODO: handle exception
System.out.println("Error");
}
return false; //没错就是对的
}
}
7.4登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String error = (String)request.getAttribute("error");
%>
<form name="form1" method="post" action="show.jsp">
<p>用户登录</p>
<p>用户名 :
<input name="username" type="text" id="username">
</p>
<p align="left"> 密码:
<input name="password" type="password" id="password" value="">
</p>
<p align="left">
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重置">
</p>
<p><%=error==null?"":error %></p>
</form>
</body>
</html>
- 注意设置编码格式
7.5 展示页面
编写第二个登录页面
<%@page import="java.util.List"%>
<%@page import="com.qzw.dao.UserDao"%>
<%@page import="com.qzw.dao.UserDao1"%>
<%@page import="com.qzw.pojo.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
UserDao u = new UserDao1();
String username = request.getParameter("username");
String password = request.getParameter("password");
//获取提交的值
if(username.equals("") || password.equals("") || !u.ifLogin(username, password)){
String error = "用户名或密码不正确!";
request.setAttribute("error",error);
request.getRequestDispatcher("login.jsp").forward(request,response);
}
List<User> list = u.getUser();
//取出所有值
%>
<p>用户<%=username %></p>
<table width="495" height="73" border="2" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<th scope="col">编号</th>
<th scope="col">姓名</th>
<th scope="col">密码</th>
</tr>
<%for(User user : list){ %>
<tr>
<td><%=user.getId() %></td>
<td><%=user.getName() %></td>
<td><%=user.getPassword() %></td>
</tr>
<%} %>
</tbody>
</table>
</body>
</html>