package Mooc;
import com.mysql.jdbc.PreparedStatement;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
//课程DAO接口
interface ICourseDao {
public int insert(Course course) throws Exception;
public int delete(int id) throws Exception;
public int update(Course course) throws Exception;
public List<Course> select() throws Exception;
}
//数据库连接工具类
class DBConnection {
// 获取数据库连接
public static Connection getConnection() throws SQLException {
//你的实现代码
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/course","root","admin");
return con;
}
// 关闭数据库资源
public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) throws Exception {
//你的实现代码
rs.close();
pstmt.close();
conn.close();
}
// 关闭数据库资源
public static void close(PreparedStatement pstmt, Connection conn) throws Exception {
//你的实现代码
pstmt.close();
conn.close();
}
}
// 课程实体类
class Course {
//你的实现代码
private int id;
private String name;
private double mark;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
}
}
//课程 DAO实现类,负责数据库访问操作的具体实现
class CourseDaoImpl implements ICourseDao {
private static final String SQL_INSERT = "insert into course (name,mark) values(?,?)";
private static final String SQL_DELETE = "delete from course where id=?";
private static final String SQL_UPDATE = "update course set name=?,mark=? where id=?";
public int insert(Course course) throws Exception {
return update(SQL_INSERT, new Object[] { course.getName(), course.getMark(), });
}
public int delete(int i) throws Exception {
return update(SQL_DELETE, new Object[] { i });
}
public int update(Course course) throws Exception {
return update(SQL_UPDATE, new Object[] { course.getName(), course.getMark(), course.getId() });
}
//查询表中所有课程信息并以列表形式返回
public List<Course> select() throws Exception {
List<Course> courseList = null;
//你的实现代码
courseList=new LinkedList<>();
Connection con=DBConnection.getConnection();
Statement stat=con.createStatement();
ResultSet rs=stat.executeQuery("select * from course");
while (rs.next()){
Course c=new Course();
c.setId(rs.getInt(1));
c.setName(rs.getString(2));
c.setMark(rs.getDouble(3));
courseList.add(c);
}
return courseList;
}
//实现课程信息的增加、修改和删除
public int update(String sql, Object[] params) throws Exception {
int flag = 0;
Connection conn = null;
PreparedStatement pstmt = null;
//你的实现代码
conn=DBConnection.getConnection();
pstmt= (PreparedStatement) conn.prepareStatement(sql);
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1,params[i]);
}
pstmt.executeUpdate();
pstmt.close();
conn.close();
return params.length;
}
}
//分别测试课程信息的查询、增加啊、修改和删除功能
class Main {
public static void main(String[] args) throws Exception {
//你的实现代码
Scanner sc=new Scanner(System.in);
CourseDaoImpl c=new CourseDaoImpl();
List<Course>courseList=c.select();
System.out.println("course现有数据:");
for(Course i:courseList){
System.out.println(i.getId()+" "+i.getName()+" "+i.getMark());
}
System.out.println("------------------------");
System.out.println("请输入你要执行的操作:\n");
System.out.println("1--增加\n2--查询\n3--修改\n4--删除\n0--退出");
int n=sc.nextInt();
while(n!=0){
switch (n){
case 1:{
Course co=new Course();
System.out.println("请输入名称:");
String sn=sc.next();
co.setName(sn);
System.out.println("请输入学分:");
Double sm=sc.nextDouble();
co.setMark(sm);
c.insert(co);
System.out.println("增加成功");
break;
}
case 2:{
System.out.println("请输入ID:");
int k=sc.nextInt();
int f=0;
for(Course i:courseList){
if(i.getId()==k){
System.out.println(i.getId()+" "+i.getName()+" "+i.getMark());
f=1;
System.out.println("查询成功");
break;
}
}if(f==0)
System.out.println("结果为空");
break;
}
case 3:{
System.out.println("请输入待修改ID:");
Course ce=new Course();
int s1=sc.nextInt();
ce.setId(s1);
System.out.println("请输入名称:");
String s2=sc.next();
ce.setName(s2);
System.out.println("请输入学分:");
ce.setMark(sc.nextDouble());
c.update(ce);
System.out.println("修改成功");
break;
}
case 4:{
System.out.println("请输入要删除的ID:");
c.delete(sc.nextInt());
System.out.println("删除成功");
break;
}
default:
System.out.println("error");
}
}
}
}