commit ffdf7dd1cd5160109fcce2cd621ea53797a8fe09
Author: zyx <1029606625@qq.com>
Date: Fri Nov 11 00:25:03 2022 +0800
2022/11/11 00:23 完成第11周作业题1
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..35410ca
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..2d3485b
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..aa00ffa
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..abb532a
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..132404b
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..511e831
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,30 @@
+
+
+ 4.0.0
+
+ cn.czyx007.week11
+ week11-work1
+ 1.0-SNAPSHOT
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+ mysql
+ mysql-connector-java
+ 8.0.30
+
+
+ org.projectlombok
+ lombok
+ 1.18.20
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/cn/czyx007/week11/Driver.java b/src/main/java/cn/czyx007/week11/Driver.java
new file mode 100644
index 0000000..7b7bcf4
--- /dev/null
+++ b/src/main/java/cn/czyx007/week11/Driver.java
@@ -0,0 +1,49 @@
+package cn.czyx007.week11;
+
+import cn.czyx007.week11.bean.Student;
+import cn.czyx007.week11.dao.impl.StudentDAOImpl;
+import cn.czyx007.week11.utils.DBUtil;
+
+import java.sql.Connection;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Scanner;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/10
+ */
+public class Driver {
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+ StudentDAOImpl studentDAO = new StudentDAOImpl();
+ Connection connection = null;
+ try {
+ connection = DBUtil.getConnection();
+ connection.setAutoCommit(false);
+ connection.commit();
+ //输入学生各项信息,保存到数据库
+ System.out.print("请输入待添加学生的学号:");
+ String id = scanner.nextLine();
+ System.out.print("请输入待添加学生的姓名:");
+ String name = scanner.nextLine();
+ System.out.print("请输入待添加学生的性别:");
+ String gender = scanner.nextLine();
+ System.out.print("请输入待添加学生的出生年月日(yyyy-MM-dd):");
+ Date birth = new SimpleDateFormat("yyyy-MM-dd").parse(scanner.nextLine());
+ System.out.print("请输入待添加学生的成绩:");
+ Double score = Double.valueOf(scanner.nextLine());
+ studentDAO.insert(new Student(id, name, gender, birth, score));
+ System.out.println("添加成功!");
+
+ // 输入学生学号,查询显示该学号学生的所有记录
+ System.out.println("请输入待查询学生的学号:");
+ id = scanner.nextLine();
+ System.out.println(studentDAO.selectById(id));
+ } catch (Exception e){
+ e.printStackTrace();
+ } finally {
+ DBUtil.close(connection, null, null);
+ }
+ }
+}
diff --git a/src/main/java/cn/czyx007/week11/bean/Student.java b/src/main/java/cn/czyx007/week11/bean/Student.java
new file mode 100644
index 0000000..48a1ed0
--- /dev/null
+++ b/src/main/java/cn/czyx007/week11/bean/Student.java
@@ -0,0 +1,22 @@
+package cn.czyx007.week11.bean;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.Date;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/9
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+@Data
+public class Student {
+ private String id;
+ private String name;
+ private String gender;
+ private Date birth;
+ private Double score;
+}
diff --git a/src/main/java/cn/czyx007/week11/dao/StudentDAO.java b/src/main/java/cn/czyx007/week11/dao/StudentDAO.java
new file mode 100644
index 0000000..90c9075
--- /dev/null
+++ b/src/main/java/cn/czyx007/week11/dao/StudentDAO.java
@@ -0,0 +1,13 @@
+package cn.czyx007.week11.dao;
+
+import cn.czyx007.week11.bean.Student;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/9
+ */
+public interface StudentDAO {
+ int insert(Student student);
+
+ Student selectById(String id);
+}
diff --git a/src/main/java/cn/czyx007/week11/dao/impl/StudentDAOImpl.java b/src/main/java/cn/czyx007/week11/dao/impl/StudentDAOImpl.java
new file mode 100644
index 0000000..43235cc
--- /dev/null
+++ b/src/main/java/cn/czyx007/week11/dao/impl/StudentDAOImpl.java
@@ -0,0 +1,66 @@
+package cn.czyx007.week11.dao.impl;
+
+import cn.czyx007.week11.bean.Student;
+import cn.czyx007.week11.dao.StudentDAO;
+import cn.czyx007.week11.utils.DBUtil;
+
+import java.sql.Connection;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/9
+ */
+public class StudentDAOImpl implements StudentDAO {
+ @Override
+ public int insert(Student student) {
+ Connection connection = null;
+ PreparedStatement ps = null;
+ int count = 0;
+ try {
+ connection = DBUtil.getConnection();
+ String sql = "insert into tstudent values(?,?,?,?,?)";
+ ps = connection.prepareStatement(sql);
+ ps.setObject(1, student.getId());
+ ps.setObject(2, student.getName());
+ ps.setObject(3, student.getGender());
+ ps.setObject(4, student.getBirth());
+ ps.setObject(5, student.getScore());
+ count = ps.executeUpdate();
+ } catch (Exception e){
+ e.printStackTrace();
+ } finally {
+ DBUtil.close(null, ps, null);
+ }
+ return count;
+ }
+
+ @Override
+ public Student selectById(String id) {
+ Connection connection = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ Student student = null;
+ try {
+ connection = DBUtil.getConnection();
+ String sql = "select * from tstudent where id = ?";
+ ps = connection.prepareStatement(sql);
+ ps.setObject(1, id);
+ rs = ps.executeQuery();
+ if(rs.next()){
+ String name = rs.getString("name");
+ String gender = rs.getString("gender");
+ Date birth = rs.getDate("birth");
+ double score = rs.getDouble("score");
+ student = new Student(id, name, gender, birth, score);
+ }
+ } catch (Exception e){
+ e.printStackTrace();
+ } finally {
+ DBUtil.close(null, ps, rs);
+ }
+ return student;
+ }
+}
diff --git a/src/main/java/cn/czyx007/week11/utils/DBUtil.java b/src/main/java/cn/czyx007/week11/utils/DBUtil.java
new file mode 100644
index 0000000..a43e7b4
--- /dev/null
+++ b/src/main/java/cn/czyx007/week11/utils/DBUtil.java
@@ -0,0 +1,70 @@
+package cn.czyx007.week11.utils;
+
+import java.sql.*;
+import java.util.ResourceBundle;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/9
+ */
+public class DBUtil {
+ //服务器上只会有一个ThreadLocal,每个线程向其中绑定自己的数据(连接对象)
+ private static ThreadLocal local = new ThreadLocal<>();
+
+ private static ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
+ private static String driver = bundle.getString("driver");
+ private static String url = bundle.getString("url");
+ private static String username = bundle.getString("username");
+ private static String password = bundle.getString("password");
+
+ //不让创建对象,因为工具类中的方法都是静态的,不需要创建对象
+ private DBUtil(){}
+
+ //DBUtil类加载时注册驱动
+ static {
+ try {
+ Class.forName(driver);
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 没有使用数据库连接池,直接创建连接对象
+ * @return 连接对象
+ * @throws SQLException
+ */
+ public static Connection getConnection() throws SQLException {
+ Connection connection = local.get();
+ if (connection == null) {
+ connection = DriverManager.getConnection(url,username,password);
+ local.set(connection);
+ }
+ return connection;
+ }
+
+ public static void close(Connection connection, PreparedStatement ps, ResultSet rs){
+ if (connection != null) {
+ try {
+ connection.close();
+ local.remove();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+ if (ps != null) {
+ try {
+ ps.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+}
diff --git a/src/main/resources/jdbc.properties b/src/main/resources/jdbc.properties
new file mode 100644
index 0000000..cb8557d
--- /dev/null
+++ b/src/main/resources/jdbc.properties
@@ -0,0 +1,4 @@
+driver=com.mysql.cj.jdbc.Driver
+url=jdbc:mysql://localhost:3306/mis?serverTimezone=UTC
+username=root
+password=123456
\ No newline at end of file