package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import mode.User;
public class JDBC {
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/tclass1?useSSL=false&serverTimezone=UTC&characterEncoding=utf8"; // 注意修改personnel为自己d数据库名
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "As838370061.";
// 1.需要修改传入的参数
// 2.需要修改sql语句
// 3.返回的内容需要修改
public static String inst(User t) {
Connection conn = null;
Statement stmt = null;
String result = "";
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "insert into user (id, name, password,email,phone) values(?,?,?,?,?)";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, t.getId());
pst.setString(2, t.getName());
pst.setString(3, t.getPassword());
pst.setString(4, t.getEmail());
pst.setString(5, t.getPhone());
pst.executeUpdate();
conn.close();
result = "success";
return result;
} catch (SQLException se) {
// 处理 JDBC 错误
result = "error";
se.printStackTrace();
return result;
} catch (Exception e) {
// 处理 Class.forName 错误
result = "error";
e.printStackTrace();
return result;
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
} // 什么都不做
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
result = "error";
se.printStackTrace();
}
}
}
public static String update(User t) {
Connection conn = null;
Statement stmt = null;
String result = "";
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "update user set name = ?, password = ?,email = ?,phone = ? where id = ?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(5, t.getId());
pst.setString(1, t.getName());
pst.setString(2, t.getPassword());
pst.setString(3, t.getEmail());
pst.setString(4, t.getPhone());
pst.executeUpdate();
conn.close();
result = "success";
return result;
} catch (SQLException se) {
// 处理 JDBC 错误
result = "error";
se.printStackTrace();
return result;
} catch (Exception e) {
// 处理 Class.forName 错误
result = "error";
e.printStackTrace();
return result;
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
} // 什么都不做
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
result = "error";
se.printStackTrace();
}
}
}
public static String delete(String id) {
Connection conn = null;
Statement stmt = null;
String result = "";
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "delete from user where id = "+id;
PreparedStatement pst = conn.prepareStatement(sql);
pst.executeUpdate();
conn.close();
result = "success";
return result;
} catch (SQLException se) {
// 处理 JDBC 错误
result = "error";
se.printStackTrace();
return result;
} catch (Exception e) {
// 处理 Class.forName 错误
result = "error";
e.printStackTrace();
return result;
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
} // 什么都不做
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
result = "error";
se.printStackTrace();
}
}
}
//1.查询所有的数据时,传入的参数为空(如果要查询某个具体的id,就需要传入参数了)
//2.要修改返回的数据类型
//3.要修改sql语句
//4.要对list数组循环赋值
public static List<User> SelectAll() {
Connection conn = null;
Statement stmt = null;
List<User> u = new ArrayList<User>();
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT id, name, email,password,phone FROM user";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while(rs.next()){
// 通过字段检索
User temp = new User();
temp.setId(rs.getString("id"));
temp.setName(rs.getString("name"));
temp.setPassword(rs.getString("password"));
temp.setPhone(rs.getString("phone"));
temp.setEmail(rs.getString("email"));
u.add(temp);
}
conn.close();
return u;
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
} // 什么都不做
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return u;
}
}