2022/11/11 00:23 完成第11周作业题1

This commit is contained in:
zyx 2022-11-11 00:25:03 +08:00
commit ffdf7dd1cd
13 changed files with 323 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

14
.idea/compiler.xml generated Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="week11-work1" />
</profile>
</annotationProcessing>
</component>
</project>

7
.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

20
.idea/jarRepositories.xml generated Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://maven.aliyun.com/repository/public" />
</remote-repository>
</component>
</project>

14
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

30
pom.xml Normal file
View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.czyx007.week11</groupId>
<artifactId>week11-work1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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<Connection> 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();
}
}
}
}

View File

@ -0,0 +1,4 @@
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mis?serverTimezone=UTC
username=root
password=123456