创建数据库
CREATE TABLE `t_user` (
`id` int NOT NULL AUTO_INCREMENT,
`account` varchar(20) NOT NULL COMMENT '账号',
`PASSWORD` varchar(64) NOT NULL COMMENT '密码',
`nickname` varchar(20) NOT NULL COMMENT '昵称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
插入数据
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6a747017daa5894beab316d6be3bad1a.png)
Java代码
package com.cheng;
import com.mysql.cj.jdbc.Driver;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
public class StatementUserLoginPart {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入账号");
String account = scanner.nextLine();
System.out.println("请输入密码");
String password = scanner.nextLine();
Class.forName("com.mysql.cj.jdbc.Driver");
Properties info = new Properties();
info.put("user", "root");
info.put("password", "123456");
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", info);
Statement statement = connection.createStatement();
String sql = "select * from test.t_user";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
int id = resultSet.getInt("id");
String account1 = resultSet.getString("account");
String password1 = resultSet.getString("password");
String nickname = resultSet.getString("nickname");
System.out.println(id + " " + account1 + " " + password1 + " " + nickname);
}
resultSet.close();
statement.close();
connection.close();
}
}
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/92281e1e82a085471d12b6f3c988b0ce.png)