首先,需要做一个准备工作——下载jar包,这个包是用来支持数据库的连接的

官网的下载链接:MySQL :: Download Connector/J

点击链接进入页面:

选择画红框的下载按钮。

与此同时,打开IDEA开发工具,在当前项目目录下新建一个lib目录文件夹用来存放第三方jar包,这样做方便管理和引用。下载完之后将jar包放进lib文件夹中,像这样:

然后点击左上角的文件—>项目结构,找到这个页面并按顺序点击红框里的按钮:

点击+号开始导入,找到刚刚保存jar的那个文件夹选择jar,在前面打上对号然后点击导入 。

这样的话就可以愉快的连接数据库了~

编写过程:

第1步,加载数据库的驱动

Class.forName("com.mysql.jdbc.Driver");

打出这一句后会报错,这时候按照软件提示来就可以,抛出或者用try...catch环绕

然后String一个数据库的表的地址:

String dburl="jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8"

这段文字的大致意思就是"jdbc:mysql://localhost:端口号/表名?是否使用SSL&字体设定=UTF-8"

再String数据库的账号和密码

String dbName = "root"; //数据库账号,默认是root
String dbPass = "******"; //数据库密码,输自己的

然后调用Connection方法进行连接

connection = DriverManager.getConnection(dburl, dbName, dbPass);

通过Connection对象获取Statement对象

statement = connection.createStatement();

到了这一步就可以定义sql语句了

String sql = "select * from users";

然后开始执行sql语句

statement.executeQuery(sql);

到这儿就是连接成功了

连接成功之后就可以为所欲为了,具体各种方法的实现我想就是通过更改每个方法中的sql语句来实现的

下面开始介绍add——添加方法

public static void addStudent() throws ClassNotFoundException, SQLException {//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");Scanner scanner = new Scanner(System.in);System.out.println("请输入学生id:");int id = scanner.nextInt();System.out.println("请输入学生姓名:");String name = scanner.next();//打开链接--连接mysqlConnection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();String sql = "INSERT INTO stu_info values("+id+",'"+name+"')";//执行sql语句statement.executeUpdate(sql);System.out.println("添加成功");connection.close();}

del——删除方法

public static void delStudent() throws ClassNotFoundException, SQLException {//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");System.out.println("连接数据库中……");Scanner scanner = new Scanner(System.in);System.out.println("请输入要删除学生的id");int delId = scanner.nextInt();//打开链接Connection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();String sql = "delete from stu_info where id = " +delId+"";statement.executeUpdate(sql);System.out.println("删除成功");connection.close();}

showAll——展示所有学生信息

public static void showAll() throws ClassNotFoundException, SQLException{//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");//打开链接Connection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();//定义sql语句String sql = "SELECT * FROM xs.stu_info;";//statement.executeUpdate(sql);ResultSet rs = statement.executeQuery(sql);while (rs.next()) {int id = rs.getInt("id");String name = rs.getString("name");System.out.println("ID:" + id);System.out.println("名字:" + name);System.out.println("==============");}}

quary——根据输入id进行查询

public static void quary() throws ClassNotFoundException, SQLException{//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");//打开链接Connection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();Scanner scanner = new Scanner(System.in);System.out.println("请输入要查询的ID");int qid = scanner.nextInt();//定义sql语句String sql = "SELECT * FROM xs.stu_info where id =" + qid +"";//statement.executeUpdate(sql);ResultSet rs = statement.executeQuery(sql);while (rs.next()) {int id = rs.getInt("id");String name = rs.getString("name");System.out.println("ID:" + id);System.out.println("名字:" + name);}

modify——修改

public static void modify() throws ClassNotFoundException, SQLException{//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");System.out.println("连接数据库中……");Scanner scanner = new Scanner(System.in);System.out.println("请输入修改前的学生id:");int id1 = scanner.nextInt();System.out.println("请输入修改后的学生id:");int id2 = scanner.nextInt();System.out.println("请输入修改后学生姓名:");String name2 = scanner.next();//打开链接--连接mysqlConnection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();String sql = "UPDATE stu_info SET id = "+id2+",name = '"+name2+"' WHERE id = "+id1+";";//执行sql语句statement.executeUpdate(sql);System.out.println("修改成功");connection.close();}

到这儿就算是大功告成了,如果想要添加更多的学生信息的话还可以在MySQL的表中添加,然后在Java的sql语句中进行具体的修改

然后再放上整体的一个代码吧

import com.mysql.jdbc.Driver;
import java.sql.*;
import java.util.Scanner;public class jdbcPro2 {public static void main(String[] args) throws SQLException, ClassNotFoundException {while (true) {System.out.println("---欢迎使用学生管理系统---");System.out.println("1-添加学生");System.out.println("2-删除学生");System.out.println("3-展示全部学生信息");System.out.println("4-查询学生");System.out.println("5-修改学生信息");System.out.println("6-退出系统");System.out.println("请输入指令:");Scanner scanner = new Scanner(System.in);String cmd = scanner.nextLine();switch (cmd) {case "1":addStudent();break;case "2":delStudent();break;case "3":showAll();break;case "4":quary();break;case "5":modify();break;case "6":System.out.println("谢谢使用");System.exit(0);Connection connection = null;Statement statement = null;ResultSet set = null;try {//1.加载数据库驱动Class.forName("com.mysql.jdbc.Driver");//2.通过DriverManager获取数据库连接String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";String dbName = "root"; //数据库账号String dbPass = "1234"; //数据库密码connection = DriverManager.getConnection(dburl, dbName, dbPass);//3.通过Connection对象获取Statement对象statement = connection.createStatement();} catch (ClassNotFoundException e) {System.out.println("错误提示:数据库驱动加载失败");e.printStackTrace();} catch (SQLException e) {System.out.println("错误提示:数据库操作失败");e.printStackTrace();} finally {if (set != null) {try {set.close();} catch (SQLException e) {e.printStackTrace();}}if (connection != null) {try {connection.close();} catch (SQLException e) {e.printStackTrace();}}if (statement != null) {try {statement.close();} catch (SQLException e) {e.printStackTrace();}}}}}}public static void addStudent() throws ClassNotFoundException, SQLException {//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");Scanner scanner = new Scanner(System.in);System.out.println("请输入学生id:");int id = scanner.nextInt();System.out.println("请输入学生姓名:");String name = scanner.next();//打开链接--连接mysqlConnection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();String sql = "INSERT INTO stu_info values("+id+",'"+name+"')";//执行sql语句statement.executeUpdate(sql);System.out.println("添加成功");connection.close();}public static void delStudent() throws ClassNotFoundException, SQLException {//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");System.out.println("连接数据库中……");Scanner scanner = new Scanner(System.in);System.out.println("请输入要删除学生的id");int delId = scanner.nextInt();//打开链接Connection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();String sql = "delete from stu_info where id = " +delId+"";statement.executeUpdate(sql);System.out.println("删除成功");connection.close();}public static void showAll() throws ClassNotFoundException, SQLException{//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");//打开链接Connection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();//定义sql语句String sql = "SELECT * FROM xs.stu_info;";//statement.executeUpdate(sql);ResultSet rs = statement.executeQuery(sql);while (rs.next()) {int id = rs.getInt("id");String name = rs.getString("name");System.out.println("ID:" + id);System.out.println("名字:" + name);System.out.println("==============");}}public static void quary() throws ClassNotFoundException, SQLException{//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");//打开链接Connection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();Scanner scanner = new Scanner(System.in);System.out.println("请输入要查询的ID");int qid = scanner.nextInt();//定义sql语句String sql = "SELECT * FROM xs.stu_info where id =" + qid +"";//statement.executeUpdate(sql);ResultSet rs = statement.executeQuery(sql);while (rs.next()) {int id = rs.getInt("id");String name = rs.getString("name");System.out.println("ID:" + id);System.out.println("名字:" + name);}}public static void modify() throws ClassNotFoundException, SQLException{//数据库地址String dburl = "jdbc:mysql://localhost:3306/xs?useSSL=false&characterEncoding=utf-8";//数据库的用户名和密码String user = "root";String password = "1234";//注册jdbc驱动Class.forName("com.mysql.jdbc.Driver");System.out.println("连接数据库中……");Scanner scanner = new Scanner(System.in);System.out.println("请输入修改前的学生id:");int id1 = scanner.nextInt();System.out.println("请输入修改后的学生id:");int id2 = scanner.nextInt();System.out.println("请输入修改后学生姓名:");String name2 = scanner.next();//打开链接--连接mysqlConnection connection = DriverManager.getConnection(dburl, user, password);Statement statement = connection.createStatement();String sql = "UPDATE stu_info SET id = "+id2+",name = '"+name2+"' WHERE id = "+id1+";";//执行sql语句statement.executeUpdate(sql);System.out.println("修改成功");connection.close();}
}

Java连接数据库(学生管理系统案例,可以实现增删改查)相关推荐

  1. python及格率公式_python案例----学生管理系统(实现学员的增删改查功能)

    学生管理系统 系统需求 1.实现可以添加新的学员 2.修改单个学员的各项信息 3.将某个学员的所有信息全部删除 4.查看单个学员的信息 5.将所有学员的信息罗列出来 6.计算考试成绩的平均值 7.计算 ...

  2. Java-spring数据库编程(idea)实现学生账号登录以及管理员增删改查功能

    通过所学的Spring数据库编程知识,实现学生管理系统的登录及增删改查的功能.要求学生在控制台输入用户名密码,如果用户账号密码正确则显示登录成功,如果登录失败则显示登录失败.登录成功后,可以进行增删改 ...

  3. IDEA2019 Java连接PostgreSQL数据库实现基础功能增删改查

    IDEA2019 Java连接PostgreSQL数据库实现基础功能增删改查 注意: 每个方法对应单个java类,可以自行进行整理汇总到一个类中 一.Java通过JDBC连接到PostgreSQL数据 ...

  4. 微信小程序小案例——简单数据增删改查模拟

    微信小程序小案例--简单数据增删改查模拟 应同学导师之邀,要做一个微信小程序,虽然没接触过,本着不会就学的态度就接了.这里就简单记录下制作过程,方便以后自己查找.(此处是粗糙版本,练习用的数据模拟)也 ...

  5. java springboot整合zookeeper入门教程(增删改查)

    java springboot整合zookeeper增删改查入门教程 zookeeper的安装与集群搭建参考:https://www.cnblogs.com/zwcry/p/10272506.html ...

  6. 学生管理系统(Java版)(普通版(增删改查)、增强版(登录、注册、忘记密码))

    普通版需求如下: 学生管理系统分为四个部分:增删改查 主菜单: 增部分:(在增加前,我们要判断id的唯一性,因此我们可以遍历ArrayList数组,再调用String的equals方法,进行判断) 删 ...

  7. Java连接sqlserver数据库,并进行增删改查操作

    用编程语言连接数据库是程序员必备的技能,今天我们就来学习一下如何通过Java来连接sqlserver数据库,并实现增删改查操作. 需要用到的工具: Myeclipse,sqlserver数据库,Mic ...

  8. 用javaweb连接数据库用javabean、severlet实现增删改查

    样 很重要的一点是建立数据库的连接 数据库是一切操作的前提 不管是增加 删除 修改 查询 都需要调用数据库连接程序 再就是java的类的编写  写完类后需要对其进行增删改查方法的 编写 这是dao层的 ...

  9. 商品管理系统商品分页,增删改查的实现增加购物车的功能Cart

    添加购物车前提条件:你的商品的页面有已经设计成功,有全选,全不选,反选这些操作的实现 通过checkbox复选框的id进行添加商品,从而获取商品的名字和其他的信息 增加成功后通过Ajax请求进行相关的 ...

  10. 图书管理系统之外键的增删改查

    ---恢复内容开始--- 1,图书管理系统的表结构设计 1.1>id,titlev,出版社_id 1.2>ORM外键:press = models.ForignKey(to="P ...

最新文章

  1. 用chrome的snippets片段功能创建页面js外挂程序,从控制台创建js小脚本
  2. You are my brother
  3. How to Increase the Memory Limit for 32-bit Applications in Windows 64-bit OS
  4. JavaScript-使用WeakMap创建对象的私有属性
  5. 工作322:uni-扩展运算符实现拼接合并操作
  6. 【CodeForces - 1042A】Benches (优先队列,思维模拟,maxmin问题)
  7. 惩罚女人的最有效方法!
  8. koajs 项目实战(二)
  9. 第五节 系统调用的三个层次(下) ——20135203齐岳
  10. 买房,有多少人帮助过你?
  11. 数据科学包16-matplotlib的三个实例
  12. 您对TOP Server的德语、中文和日语语言支持了解吗?(一)
  13. python中关于图例legend在图外的画法简析
  14. Android studio 权限大全
  15. python 拼音输入法_用Python从头开始实现一个中文拼音输入法?
  16. python手机号归属地查询
  17. android获取角速度,android dyrscope sensor 角速度传感器调试
  18. html中多一条黑线,Word页面中上下各有一条黑线怎样去掉?
  19. Camera+收入超500万美金,VPlayer能否击败其神话?
  20. 随笔:《像火箭科学家一样思考:将不可能变成可能》观书有感

热门文章

  1. sas9.4安装教程
  2. linux下跑分软件下载,geekbench5下载-多平台综合性测试工具 v5.3.1 免费版 - 下载吧...
  3. CocosCreator休闲游戏发布到字节跳动平台
  4. kali字典爆破wifi密码
  5. DSP28335串口打印 printf
  6. uat测试用例和sit测试用例_测试理论——SIT测试 和 UAT测试概念
  7. SQL Server查询语句
  8. 2022爱分析・汽车行业数字化实践报告
  9. 运维系统 联想服务器,联想IT综合运维平台解决方案.pdf
  10. 自制小型USB TO TTL串口工具