IDEA中Java实现连接MySQL数据库以及对数据库的增删改查 您所在的位置:网站首页 navicat怎么删除数据库连接 IDEA中Java实现连接MySQL数据库以及对数据库的增删改查

IDEA中Java实现连接MySQL数据库以及对数据库的增删改查

2023-11-25 16:59| 来源: 网络整理| 查看: 265

目录 1开发环境及安装2创建数据库2.1在终端中登录和创建数据库(本地)2.2创建数据表2.3mysql的数据类型2.3.1数值类型2.3.2字符串2.3.3日期和时间类型 2.4向数据表中插入数据 3在IDEA中创建数据库的连接5查询数据6添加数据7删除数据8改变数据9整个Java文件代码如下

1开发环境及安装

Windows 10系统 IDEA:自行百度 MySQL: 下载和安装见此链接链接.

2创建数据库

参考链接链接点击此处

2.1在终端中登录和创建数据库(本地)

登录mysql的指令

mysql -h 主机名 -u 用户名 -p

参数说明:

-h : 指定客户端所要登录的 MySQL 主机名, 登录本机(localhost 或 127.0.0.1)该参数可以省略;-u : 登录的用户名;-p : 告诉服务器将会使用一个密码来登录, 如果所要登录的用户名密码为空, 可以忽略此选项。

如果我们要登录本机的 MySQL 数据库,只需要输入以下命令即可:

C:\Users\LXQ>mysql -u root -p

注意: 在输入密码时,密码是不会显示了,你正确输入即可。

查看mysql中的所有数据库

mysql> show DATABASES;

在这里插入图片描述 这里是一些默认的数据库,接下来创建新的数据库

mysql> create DATABASE lxq;

此时lxq数据库已经创建,切换到lxq数据库中

mysql> use lxq; 2.2创建数据表 mysql> CREATE TABLE tab1( -> id INT NOT NULL AUTO_INCREMENT, -> title VARCHAR(100) NOT NULL, -> author VARCHAR(40) NOT NULL, -> submission_date DATE, -> PRIMARY KEY(id) -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;

参数说明

2.3mysql的数据类型 2.3.1数值类型

在这里插入图片描述

2.3.2字符串

在这里插入图片描述

2.3.3日期和时间类型

在这里插入图片描述

2.4向数据表中插入数据 mysql> INSERT INTO tab1 -> (title, author, submission_date) -> VALUES -> ("learning", "xiaoli", "2020-10-28");

打开Workbench可以看到数据已成功插入 在这里插入图片描述

3在IDEA中创建数据库的连接

代码如下

import java.sql.*; public class Conn { public static void main(String[] args) { Connection con; // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL // String driver = "com.mysql.jdbc.Driver"; // String url = "jdbc:mysql://localhost:3306/RUNOOB"; // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL String driver="com.mysql.cj.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/lxq?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; // 数据库的用户名与密码,需要根据自己的设置 String user="root"; String password="1234"; try { //注册JDBC驱动 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } con.close(); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } } public void queryDb(){ } }

数据库中的tab1数据表 在这里插入图片描述

5查询数据 public static void queryDb(){ Connection con; try { //注册JDBC驱动程 序 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = con.createStatement(); // String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')"; String sql = "select title,author,submission_date from tab1"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()){ String title = rs.getString("title"); String author = rs.getString("author"); System.out.println("title : " + title); System.out.println("author : " + author); } con.close(); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } }

输出结果 在这里插入图片描述

6添加数据 public static void insertDb(){ Connection con; try { //注册JDBC驱动程 序 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = con.createStatement(); // 必须要写入id值,否则插入错误,建立数据库时,不是设置id值自动,为何还必须写入??? String sql = "insert into tab1 values ('3', 'li','xiao','2020-07-28')"; stmt.executeUpdate(sql); con.close(); System.out.println("insert success!"); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } }

添加数据后数据表中的变化 在这里插入图片描述

7删除数据 public static void deleteDb(){ Connection con; try { //注册JDBC驱动程 序 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = con.createStatement(); // 必须要写入id值,否则插入错误,建立数据库时,不是设置id值自动,为何还必须写入??? String sql = "delete from tab1 where id='3'"; stmt.executeUpdate(sql); con.close(); System.out.println("delete success!"); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } }

删除数据后数据表中的变化 在这里插入图片描述

8改变数据 public static void changeDb(){ Connection con; try { //注册JDBC驱动程 序 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = con.createStatement(); // 必须要写入id值,否则插入错误,建立数据库时,不是设置id值自动,为何还必须写入??? String sql = "update tab1 set title='xihaxiha' where id='2'"; stmt.executeUpdate(sql); con.close(); System.out.println("change success!"); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } }

改变数据后数据库的变化 在这里插入图片描述

9整个Java文件代码如下 import java.sql.*; public class ConnectionTest { final static String driver="com.mysql.cj.jdbc.Driver"; //这里我的数据库是cxxt final static String url="jdbc:mysql://localhost:3306/lxq?&useSSL=false&serverTimezone=UTC"; final static String user="root"; final static String password="1234"; public static void main(String[] args){ // insertDb(); // queryDb(); // deleteDb(); changeDb(); } public static void queryDb(){ Connection con; try { //注册JDBC驱动程 序 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = con.createStatement(); // String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')"; String sql = "select title,author,submission_date from tab1"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()){ String title = rs.getString("title"); String author = rs.getString("author"); System.out.println("title : " + title); System.out.println("author : " + author); } con.close(); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } } public static void insertDb(){ Connection con; try { //注册JDBC驱动程 序 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = con.createStatement(); // 必须要写入id值,否则插入错误,建立数据库时,不是设置id值自动,为何还必须写入??? String sql = "insert into tab1 values ('3', 'li','xiao','2020-07-28')"; stmt.executeUpdate(sql); con.close(); System.out.println("insert success!"); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } } public static void deleteDb(){ Connection con; try { //注册JDBC驱动程 序 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = con.createStatement(); // 必须要写入id值,否则插入错误,建立数据库时,不是设置id值自动,为何还必须写入??? String sql = "delete from tab1 where id='3'"; stmt.executeUpdate(sql); con.close(); System.out.println("delete success!"); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } } public static void changeDb(){ Connection con; try { //注册JDBC驱动程 序 Class.forName(driver); //建立连接 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = con.createStatement(); // 必须要写入id值,否则插入错误,建立数据库时,不是设置id值自动,为何还必须写入??? String sql = "update tab1 set title='xihaxiha' where id='2'"; stmt.executeUpdate(sql); con.close(); System.out.println("change success!"); } catch (ClassNotFoundException e) { System.out.println("数据库驱动没有安装"); } catch (SQLException e) { e.printStackTrace(); System.out.println("数据库连接失败"); } } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有