package com.example.java_algorithms;
import com.mysql.cj.jdbc.Driver;
import io.swagger.models.auth.In;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;
public class JDBC {
public static Connection connectMysql() {
String url="jdbc:mysql://localhost:3306/monitor?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
String driverName="com.mysql.cj.jdbc.Driver";
String username="root";
String password="123456";
try {
Class.forName(driverName);
Connection connection=DriverManager.getConnection(url,username,password);
System.out.println("数据库连接成功!");
return connection;
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}catch (SQLException e){
e.printStackTrace();
System.out.println("数据库连接失败!");
}
return null;
}
static List<HashMap<String,Object>> selectAllUser(Connection connection){
String sql="SELECT id,email,password FROM t_user";
try {
PreparedStatement preparedStatement=connection.prepareStatement(sql);
System.out.println("执行成功:");
ResultSet resultSet=preparedStatement.executeQuery(sql);
List<HashMap<String,Object>> hashMapList=new ArrayList<>();
while (resultSet.next()){
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("id",resultSet.getInt("id"));
hashMap.put("email",resultSet.getString("email"));
hashMap.put("password",resultSet.getString("password"));
hashMapList.add(hashMap);
}
return hashMapList;
}catch (SQLException e){
e.printStackTrace();
return null;
}
}
public static void addUser(Connection connection,HashMap<String,Object>hashMap){
String sql = "INSERT INTO t_user(email,password) VALUES(?,?)";
try {
PreparedStatement preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,hashMap.get("email").toString());
preparedStatement.setString(2,hashMap.get("password").toString());
int i=preparedStatement.executeUpdate();
if(i>0){
System.out.println("添加成功");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void delete(Connection connection,String email){
String sql ="DELETE FROM t_user WHERE email=?";
try {
PreparedStatement preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,email);
int i=preparedStatement.executeUpdate();
if(i>0) {
System.out.println("删除成功");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void updateUser(Connection connection,String email,String password){
String sql="UPDATE t_user SET password=? WHERE email =?";
try {
PreparedStatement preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,password);
preparedStatement.setString(2,email);
int i=preparedStatement.executeUpdate();
if(i>0){
System.out.println("修改成功!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Connection connection=connectMysql();
List<HashMap<String,Object>> hashMapList=selectAllUser(connection);
System.out.println("查询所有:"+hashMapList);
HashMap<String,Object> addHashMap=new HashMap<>();
addHashMap.put("email","小红");
addHashMap.put("password","123456");
addUser(connection,addHashMap);
delete(connection,"小环");
updateUser(connection,"小红","123456789000");
}
}
|