archieve: homework7 test1 test2
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* @name: User.java
|
||||
* @author: msksbr
|
||||
* @date: 2025-05-26
|
||||
* @version: 1.0
|
||||
* @description: User class for homework 7
|
||||
*/
|
||||
package com.msksbr.homework7.domain;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class User {
|
||||
private int id;
|
||||
private String username;
|
||||
private String password;
|
||||
private String email;
|
||||
private Date birthday;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* @name: JDBCUtils.java
|
||||
* @author: msksbr
|
||||
* @date: 2025-05-26
|
||||
* @version: 1.0
|
||||
* @description: JDBCUtils class for homework 7
|
||||
*/
|
||||
package com.msksbr.homework7.util;
|
||||
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.Map;
|
||||
|
||||
public class JDBCUtils {
|
||||
private static final DBInfo dbInfo = new DBInfo();
|
||||
private Connection connection;
|
||||
private Statement statement;
|
||||
|
||||
public JDBCUtils() {
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
connection = java.sql.DriverManager.getConnection(dbInfo.getUrl(), dbInfo.username, dbInfo.password);
|
||||
statement = connection.createStatement();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet runQuery(String query) {
|
||||
try {
|
||||
return this.statement.executeQuery(query);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class DBInfo {
|
||||
String address;
|
||||
String port;
|
||||
String username;
|
||||
String password;
|
||||
String database;
|
||||
String yaml_url = "/dbInfo.yaml";
|
||||
|
||||
public DBInfo() {
|
||||
Map<String, String> dbInfo = loadYaml();
|
||||
this.address = dbInfo.get("address");
|
||||
this.port = dbInfo.get("port");
|
||||
this.username = dbInfo.get("username");
|
||||
this.password = dbInfo.get("password");
|
||||
this.database = dbInfo.get("database");
|
||||
}
|
||||
|
||||
public Map<String, String> loadYaml() {
|
||||
Yaml yaml = new Yaml();
|
||||
try (InputStream inputStream = DBInfo.class.getResourceAsStream(yaml_url)) {
|
||||
if (inputStream == null) {
|
||||
throw new RuntimeException("YAML file not found" + yaml_url);
|
||||
}
|
||||
return yaml.load(inputStream);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to load YAML file", e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
String url = "jdbc:mysql://" + this.address + ":" + this.port + "/" + this.database;
|
||||
return url;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# this is a sample of yaml file for dbInfo.yaml
|
||||
# you can change the value of the key to your own value
|
||||
# 1. copy this file to the same directory as dbInfo.yaml
|
||||
# 2. change the value of the key to your own value
|
||||
address: "localhost"
|
||||
port: "3306"
|
||||
username: "root"
|
||||
password: "123456"
|
||||
database: "master"
|
||||
@@ -0,0 +1,9 @@
|
||||
import com.msksbr.homework7.util.JDBCUtils;
|
||||
|
||||
public class testSQL {
|
||||
public static void main(String[] args) {
|
||||
JDBCUtils jdbcUtils = new JDBCUtils();
|
||||
String query = "SELECT * FROM users";
|
||||
System.out.println(jdbcUtils.runQuery(query));
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,8 @@
|
||||
# you can change the value of the key to your own value
|
||||
# 1. copy this file to the same directory as dbInfo.yaml
|
||||
# 2. change the value of the key to your own value
|
||||
link:
|
||||
address: "localhost"
|
||||
port: 3306
|
||||
username: "root"
|
||||
password: "123456"
|
||||
database: "master"
|
||||
address: "localhost"
|
||||
port: 3306
|
||||
username: "root"
|
||||
password: "123456"
|
||||
database: "master"
|
||||
Reference in New Issue
Block a user