当前位置:网站首页>Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
2022-07-05 17:01:00 【Oh-liuxing】
package Mooc;
import com.mysql.jdbc.PreparedStatement;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
// Course DAO Interface
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;
}
// Database connection utility class
class DBConnection {
// Get database connection
public static Connection getConnection() throws SQLException {
// Your implementation code
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/course","root","admin");
return con;
}
// Close the database resource
public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) throws Exception {
// Your implementation code
rs.close();
pstmt.close();
conn.close();
}
// Close the database resource
public static void close(PreparedStatement pstmt, Connection conn) throws Exception {
// Your implementation code
pstmt.close();
conn.close();
}
}
// Course entity class
class Course {
// Your implementation code
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;
}
}
// Course DAO Implementation class , Be responsible for the specific implementation of database access operation
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() });
}
// Query all course information in the table and return it in the form of a list
public List<Course> select() throws Exception {
List<Course> courseList = null;
// Your implementation code
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;
}
// Realize the increase of course information 、 Modification and deletion
public int update(String sql, Object[] params) throws Exception {
int flag = 0;
Connection conn = null;
PreparedStatement pstmt = null;
// Your implementation code
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;
}
}
// Test the query of course information separately 、 Increase 、 Modify and delete functions
class Main {
public static void main(String[] args) throws Exception {
// Your implementation code
Scanner sc=new Scanner(System.in);
CourseDaoImpl c=new CourseDaoImpl();
List<Course>courseList=c.select();
System.out.println("course Available data :");
for(Course i:courseList){
System.out.println(i.getId()+" "+i.getName()+" "+i.getMark());
}
System.out.println("------------------------");
System.out.println(" Please enter the operation you want to perform :\n");
System.out.println("1-- increase \n2-- Inquire about \n3-- modify \n4-- Delete \n0-- sign out ");
int n=sc.nextInt();
while(n!=0){
switch (n){
case 1:{
Course co=new Course();
System.out.println(" Please enter name :");
String sn=sc.next();
co.setName(sn);
System.out.println(" Please enter credits :");
Double sm=sc.nextDouble();
co.setMark(sm);
c.insert(co);
System.out.println(" Increase success ");
break;
}
case 2:{
System.out.println(" Please enter 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(" The query is successful ");
break;
}
}if(f==0)
System.out.println(" The result is empty. ");
break;
}
case 3:{
System.out.println(" Please enter to be modified ID:");
Course ce=new Course();
int s1=sc.nextInt();
ce.setId(s1);
System.out.println(" Please enter name :");
String s2=sc.next();
ce.setName(s2);
System.out.println(" Please enter credits :");
ce.setMark(sc.nextDouble());
c.update(ce);
System.out.println(" Modification successful ");
break;
}
case 4:{
System.out.println(" Please enter the... To be deleted ID:");
c.delete(sc.nextInt());
System.out.println(" Delete successful ");
break;
}
default:
System.out.println("error");
}
}
}
}

边栏推荐
- Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
- Benji Bananas 会员通行证持有人第二季奖励活动更新一览
- PHP人才招聘系统开发 源代码 招聘网站源码二次开发
- 【微信小程序】一文读懂小程序的生命周期和路由跳转
- [Jianzhi offer] 61 Shunzi in playing cards
- 美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
- Jarvis OJ 远程登录协议
- Jarvis OJ shell traffic analysis
- Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
- Jarvis OJ Flag
猜你喜欢

Hiengine: comparable to the local cloud native memory database engine

Cs231n notes (bottom) - applicable to 0 Foundation

Solution of vant tabbar blocking content

阈值同态加密在隐私计算中的应用:解读

Global Data Center released DC brain system, enabling intelligent operation and management through science and technology

China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users

项目引入jar从私服Nexus 拉去遇到的一个问题

Benji Bananas 会员通行证持有人第二季奖励活动更新一览

【刷题篇】有效的数独

干货!半监督预训练对话模型 SPACE
随机推荐
Jarvis OJ Webshell分析
[echart] resize lodash to realize chart adaptation when window is zoomed
ECU简介
PHP 严格模式
Jarvis OJ shell traffic analysis
如何将mysql卸载干净
【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
Deep learning plus
Jarvis OJ shell流量分析
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
Yarn common commands
C how TCP restricts the access traffic of a single client
How was the middle table destroyed?
深潜Kotlin协程(二十一):Flow 生命周期函数
Application of threshold homomorphic encryption in privacy Computing: Interpretation
项目引入jar从私服Nexus 拉去遇到的一个问题
【机器人坐标系第一讲】
Wsl2.0 installation
帮忙看看是什么问题可以吗?[ERROR] Could not execute SQL stateme