From f65b48b374418dcb8428bf6fe6585a67e7c106da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E3=82=B9=E3=83=90=E3=83=AB?= Date: Mon, 26 May 2025 13:05:54 +0800 Subject: [PATCH] archieve: homework7 test1 test2 --- .../com/msksbr/homework7/domain/User.java | 58 ++++++++++++++ .../com/msksbr/homework7/util/JDBCUtils.java | 77 +++++++++++++++++++ .../src/main/resources/dbInfo sample.yaml | 9 +++ homework7/src/test/java/testSQL.java | 9 +++ .../com/msksbr/test/jdbc/dbInfo sample.yaml | 11 ++- 5 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 homework7/src/main/java/com/msksbr/homework7/domain/User.java create mode 100644 homework7/src/main/java/com/msksbr/homework7/util/JDBCUtils.java create mode 100644 homework7/src/main/resources/dbInfo sample.yaml create mode 100644 homework7/src/test/java/testSQL.java diff --git a/homework7/src/main/java/com/msksbr/homework7/domain/User.java b/homework7/src/main/java/com/msksbr/homework7/domain/User.java new file mode 100644 index 0000000..0a787c9 --- /dev/null +++ b/homework7/src/main/java/com/msksbr/homework7/domain/User.java @@ -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; + } +} diff --git a/homework7/src/main/java/com/msksbr/homework7/util/JDBCUtils.java b/homework7/src/main/java/com/msksbr/homework7/util/JDBCUtils.java new file mode 100644 index 0000000..e948a47 --- /dev/null +++ b/homework7/src/main/java/com/msksbr/homework7/util/JDBCUtils.java @@ -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 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 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; + } + } +} + diff --git a/homework7/src/main/resources/dbInfo sample.yaml b/homework7/src/main/resources/dbInfo sample.yaml new file mode 100644 index 0000000..b04596c --- /dev/null +++ b/homework7/src/main/resources/dbInfo sample.yaml @@ -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" \ No newline at end of file diff --git a/homework7/src/test/java/testSQL.java b/homework7/src/test/java/testSQL.java new file mode 100644 index 0000000..8125075 --- /dev/null +++ b/homework7/src/test/java/testSQL.java @@ -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)); + } +} diff --git a/test/jdbc/src/main/resources/com/msksbr/test/jdbc/dbInfo sample.yaml b/test/jdbc/src/main/resources/com/msksbr/test/jdbc/dbInfo sample.yaml index 5e5c243..ae4b397 100644 --- a/test/jdbc/src/main/resources/com/msksbr/test/jdbc/dbInfo sample.yaml +++ b/test/jdbc/src/main/resources/com/msksbr/test/jdbc/dbInfo sample.yaml @@ -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" \ No newline at end of file