利用JDBC技术和mysql数据库管理系统实现课程管理功能,包括课程信息的新增、修改、查询和删除。

本文主要是介绍利用JDBC技术和mysql数据库管理系统实现课程管理功能,包括课程信息的新增、修改、查询和删除。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

 

 

 

 

 

 

这篇关于利用JDBC技术和mysql数据库管理系统实现课程管理功能,包括课程信息的新增、修改、查询和删除。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/539018

相关文章

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串