Java连接MySQL数据库具体操作

article/2025/8/21 5:46:17

一,主要分为5个步骤

1,获取驱动
首先下载自己mysql版本的jar包
下载地址:MySQL :: Download Connector/J
将下载好的jar包复制到jdk的安装目录下的jre下的lib下的ext目录下
mysql.5及以下的版本获取驱动是Class.forName("com.mysql.jdbc.Driver");
mysql.8及以上的版本获取驱动是Class.forName("com.mysql.cj.jdbc.Driver");
2,连接数据库
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db0613?useUnicode=true&characterEncoding=utf8","root","123");

db0613是自己创建的数据库,root表示数据库用户名,123表示数据库密码

?useUnicode=true&characterEncoding=utf8 编码集
3,创建命令执行对象
Statement statement = connection.createStatement();
String sqlUpdate = "delete from student where stu_id = 1001";
String sqlQuery = "select * from student";
4,通过执行对象执行SQL语句
statement.executeUpdate(sqlUpdate);  //增删改
ResultSet resultSet = statement.executeQuery(sqlQuery);  //查询
5,释放资源
if(resultSet != null){
  resultSet.close();
}
if(statement != null){
  statement.close();
}
if(connection != null){
  connection.close();
}

 二,具体的代码实现

1,增删改

package com.jdbc.test;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*** 增删改操作*/
public class UpdateJdbc {public static void main(String[] args) throws ClassNotFoundException, SQLException {//加载驱动Class.forName("com.mysql.jdbc.Driver");System.out.println("加载驱动成功");//连接数据库Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db0613?useUnicode=true&characterEncoding=utf8","root","123");System.out.println("连接数据库成功");//创建命令执行对象Statement stmt = conn.createStatement();String sql = "insert into student values(1007,'小春','1','1900-2-2','0','0','0')";System.out.println("创建命令执行对象成功");//执行对象执行SQL语句stmt.executeUpdate(sql);System.out.println("执行对象执行SQL语句成功");//关闭资源if (stmt != null) {stmt.close();stmt=null;}if (conn != null) {conn.close();conn=null;}System.out.println("关闭资源成功");}
}

2,查询

package com.jdbc.test;import com.jdbc.bean.Student;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;/*** 查询操作*/
public class QueryJdbc {public static void main(String[] args) throws ClassNotFoundException, SQLException {//加载驱动Class.forName("com.mysql.jdbc.Driver");System.out.println("加载驱动成功");//连接数据库Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db0613?useUnicode=true&characterEncoding=utf8","root","123");System.out.println("连接数据库成功");//创建命令执行对象Statement stmt = conn.createStatement();String sql = "select * from student";System.out.println("创建命令执行对象成功");//执行对象执行SQL语句//ResultSet只保存了一个地址ResultSet rs = stmt.executeQuery(sql);List<Student> list = new ArrayList<>();while (rs.next()) {int stu_id = rs.getInt("stu_id");String stu_name = rs.getString("stu_name");String stu_sex = rs.getString("stu_sex");Date stu_both = rs.getDate("stu_both");String stu_addr = rs.getString("stu_addr");String stu_rec = rs.getString("stu_rec");String stu_pwd = rs.getString("stu_pwd");Student student = new Student(stu_id,stu_name,stu_sex,stu_both,stu_addr,stu_rec,stu_pwd);list.add(student);}System.out.println(list.size());System.out.println("执行对象执行SQL语句成功");//关闭资源if (rs != null) {rs.close();rs = null;}if (stmt != null) {stmt.close();stmt = null;}if (conn != null) {conn.close();conn=null;}System.out.println("关闭资源成功");}
}

3,封装后的增删改查

package com.jdbc.dao;import java.sql.*;
import java.util.ArrayList;
import java.util.List;/****/
public class BaseDao<E> {private static final String DRIVER = "com.mysql.jdbc.Driver";private static final String URL = "jdbc:mysql://localhost:3306/db0613?useUnicode=true&characterEncoding=utf8";private static final String USERNAME = "root";private static final String PASSWORD = "123";private Connection conn  = null;private Statement stmt = null;protected ResultSet rs = null;//加载驱动,只加载一次static {try {Class.forName(DRIVER);} catch (ClassNotFoundException e) {throw new RuntimeException(e);}}//连接数据库public void getConn() {try {conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);} catch (SQLException e) {throw new RuntimeException(e);}}//创建命令执行对象public void getStmt() {try {stmt = conn.createStatement();} catch (SQLException e) {throw new RuntimeException(e);}}//关闭资源public void closeRes() {if (rs != null) {try {rs.close();} catch (SQLException e) {throw new RuntimeException(e);}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {throw new RuntimeException(e);}}if (conn != null) {try {conn.close();} catch (SQLException e) {throw new RuntimeException(e);}}}//增删改public void update(String sql) throws Exception {getConn();getStmt();stmt.executeUpdate(sql);closeRes();}//查询public List<E> query(String sql) throws SQLException {getConn();getStmt();rs = stmt.executeQuery(sql);List<E> list = new ArrayList<>();rsList(list);closeRes();return list;}public void rsList(List<E> list) throws SQLException {}
}

4,具体的实现

package com.jdbc.dao;import com.jdbc.bean.Student;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;/****/
public class StudentDao extends BaseDao<Student>{public List<Student> findAll() throws SQLException {String sql = "select * from student";return super.query(sql);}public List<Student> findStudentById(String stu_id) throws SQLException {String sql = "select * from student where stu_id = "+stu_id+"";return super.query(sql);}public List<Student> addStudent(Student stu) throws SQLException {String sql = "insert into student values('"+stu.getStu_id()+"')";return super.query(sql);}@Overridepublic void rsList(List<Student> list) throws SQLException {while (rs.next()) {int stu_id = rs.getInt("stu_id");String stu_name = rs.getString("stu_name");String stu_sex = rs.getString("stu_sex");Date stu_both = rs.getDate("stu_both");String stu_addr = rs.getString("stu_addr");String stu_rec = rs.getString("stu_rec");String stu_pwd = rs.getString("stu_pwd");Student student = new Student(stu_id,stu_name,stu_sex,stu_both,stu_addr,stu_rec,stu_pwd);list.add(student);}}
}

5,测试类

package com.jdbc.test;import com.jdbc.bean.Student;
import com.jdbc.dao.StudentDao;
import java.util.List;/*** 测试*/
public class Test1 {public static void main(String[] args) throws Exception {String sql = "select * from student";List<Student> list = new StudentDao().findAll();for(Student lists : list){System.out.print(lists.getStu_id()+"\t\t");System.out.print(lists.getStu_name()+"\t\t");System.out.print(lists.getStu_sex()+"\t\t");System.out.print(lists.getStu_both()+"\t\t");System.out.print(lists.getStu_addr()+"\t\t");System.out.print(lists.getStu_rec()+"\t\t");System.out.println(lists.getStu_pwd()+"\t\t");}}
}

6,测试结果

 


http://chatgpt.dhexx.cn/article/R57v1bSj.shtml

相关文章

MySQL学习(五)JDBC 使用java操作数据库

目录 1.JDBC基本原理 2.数据库驱动包的导入&#xff08;环境配置&#xff09; 3.JDBC的使用步骤 4.JDBC的常用接口和抽象类 5.核心小结&#xff1a; 1.JDBC基本原理 现在&#xff0c;要使用java操作数据库&#xff0c;则我们的项目的构建或者说代码的组成增加为了三方&…

Java JDBC操作MySQL

DBMS里有多个数据库&#xff0c;每个库中又有多张表。 客户端有命令终端(DoS)、SQLyog、Java等。 Mysql数据库、普通表的本质仍然是文件。 表的一行称为一条记录&#xff0c;对应Java中的一个对象。 SQL语句的分类为 1、DDL数据定义语言 create 表、库 2、DML 数据操作语言 in…

Java对MySQL数据库进行操作【java】

数据库几乎是每一个应用型的程序都会用到&#xff0c;学会如何高效操作数据库也是开发一个重点。今天我总结了具体的操作步骤以及注意点。Java访问数据库主要用的方法是JDBC&#xff0c;它是java语言中用来规范客户端程序如何来访问数据库的应用程序接口&#xff0c;提供了诸如…

Java 中使用 MySql以及一些Mysql 基本的命令

Linux mint 环境下配置 mysql 打开终端输入 sudo apt-get install mysql-server,回车即可安装 mysql&#xff0c;当然如果你想使用最新的 mysql&#xff0c;可以去官方下载并且配置&#xff0c;教程网上有很多&#xff0c;就不再赘述。 安装完成之后&#xff0c;在终端中输入…

【java对MySQL的操作详解】java学习之增查

文章目录 前言一&#xff0c;封装JDBC二&#xff0c;增2.1&#xff0c;UserDAO.java2,2&#xff0c;UserServier.java2,3&#xff0c;userUI.java添加用户 三&#xff0c;查3.1&#xff0c;UserUI伪iu3.2&#xff0c;UserServier.java3.3&#xff0c;UserDAO.java END 前言 具…

在Java中操作MySQL(JDBC)

准备工作 ideamysql 官方的 jdbc 驱动 先在 idea 中建立一个新的 java 工程 直到这一步之前&#xff0c;一直按下一步就行了 把名字改改直接 finish 在这个工程下面建立一个名字为 lib 的空文件夹 去官方下载依赖的包 https://dev.mysql.com/downloads/connector/j/ 选…

java操作mysql数据库

java操作mysql数据库 学习到数据库操作这一章&#xff0c;遇到了很多问题&#xff0c;只能说无论什么时候&#xff0c;用什么工具来操作数据库&#xff0c;都不是一件简单的事情。 简单记录一下 数据库连接 先看代码界面 要连接数据库&#xff0c;首先需要导入mysql依赖包&…

用Java操作MySQL

用Java操作MySQL目录 一、创建数据库1、建库2、建表 二、开始准备jdbc1、导入mysql驱动jar包2、开始写jdbc代码 三、那就运行吧附&#xff1a;最终代码DBUtilMaindb.properties 一、创建数据库 打开Navicat&#xff08;我用的是DataGrip&#xff09;的查询控制台 1、建库 sq…

c++ opencv 身份证OCR识别:前期数据训练库准备(tesseractOCR)

前言&#xff1a;本文已经在个人博客更新排版和讲解【传送门】 整个教程已经在有道云写好了&#xff0c;在博客要重新把每个图片再上传一次有点累&#xff0c;先截了图直接看吧&#xff01;如果有需要有提供word版本可以下载 https://download.csdn.net/download/stopping5/11…

身份证正反面识别,身份证扫描识别,二代身份证OCR识别,OCR极速识别身份证所有信息正反面均可。离线无需联网,极速秒扫。

项目说明 极速识别二代身份证、驾驶证、护照 (无需联网&#xff0c;离线秒扫&#xff0c;极速识别)身份证所有信息, 包含姓名、性别、出生年月、详细地址&#xff0c;正反面。不需要联网即可离线识别身份证所有信息包括新疆少数民族身份证&#xff0c;识别速度快&#xff0c;识…

如何对接身份证OCR?

【身份证OCR文字识别】身份证OCR识别-身份证识别-身份证-OCR文字-身份证OCR文字-身份证OCR-身份证OCR图像识别【最新版】_图像识别_身份证识别_OCR-云市场-阿里云 身份证OCR免费体验 Java代码如下 public static void main(String[] args) {String host "https://idcar…

实名二代身份证OCR识别

在银行、信用社、航空、保险、电信等部门单位办理手续或证件时&#xff0c;常需要身份证件&#xff0c;但业务人员手工录入速度慢、办公效率太低是一大亟需解决的问题&#xff0c;通常要先通过扫描或拍照&#xff0c;来获取身份证上面的头像&#xff0c;然后再手工填写用户的身…

Opencv实现身份证OCR识别

Opencv 配置IDEA可参考&#xff1a;https://blog.csdn.net/zwl18210851801/article/details/81075781 opencv位置&#xff1a; OpencvUtil类&#xff1a; package com.x.common.utils;import org.opencv.core.*; import org.opencv.core.Point; import org.opencv.imgcodecs.…

身份证OCR实名认证接口

人工智能&#xff0c;身份证&#xff0c;OCR实名验证 一、接口介绍 自动识别及快速提取二代居民身份证正反面所有字段&#xff0c;包括姓名、性别、民族、出生日期、住址、身份证号、签发机关、有效期限&#xff0c;实时联网核查校验是否一致。对URL网络图片或base64信息进行…

身份证OCR识别SDK

一、身份证OCR识别SDK应用背景 随着Android、IOS智能手机的普及&#xff0c;5G通信技术的发展&#xff0c;移动互联迎来了爆发式增长&#xff0c;越来越多的公司都推出了自己的移动APP、小程序等应用。这些应用多数都涉及到个人身份证信息的输入认证&#xff08;即实名认证&…

阿里云智能身份证OCR文字识别

智能身份证OCR文字识别 做的项目里面涉及到了身份证识别输入的真实姓名与身份证号码是否匹配功能&#xff0c;于是在阿里云接了智能身份证OCR文字识别API&#xff0c;登录阿里云找到对应的产品&#xff0c;如下图 购买使用然后生成对应的APPcode。 使用的demo有蛮多&#xff0c…

如何用 身份证OCR 接口进行快速开发

最近公司项目有一个身份证文字识别的小需求&#xff0c;想着如果用现成的API就可以大大提高开发效率&#xff0c;在网上的API商店搜索了一番&#xff0c;发现了 APISpace&#xff0c;它里面的身份证OCR非常符合我的开发需求。 身份证OCR&#xff0c;传入身份证照片&#xff0c…

离线身份证OCR识别

离线身份证ocr识别简介 离线身份证ocr识别SDK可支持Android、iOS主流移动操作系统&#xff0c;android平台提供jar包&#xff0c;ios提供静态库.a文件。APP集成离线身份证ocr识别SDK后&#xff0c;用户采用手机、平板电脑对身份证进行拍摄识别即可自动识别身份证信息。 离线身…

身份证OCR识别

本文章来源于自己的学习图像处理以及识别过程中的一些经验&#xff0c;开发了一个OCR识别身份证程序&#xff0c;此程序虽不够完美但有一定的参考价值&#xff0c;CSDN上本人也将提供源代码&#xff0c;对身份证OCR识别整个过程的一个梳理。 程序依赖库&#xff0c;OpenCV3.20…

4款堪称手机神器的APP,每个都可以玩转手机,尤其是第三个

每个人的手机中都会一到两个永远不会卸载的APP&#xff0c;有些APP可以让你在工作中事半功倍&#xff0c;有些可以让你打发时间&#xff0c;并且还可以学习到不同的知识。下面就为大家介绍几个即可以娱乐还可以学习的APP。 一周计划 对于记忆力不好的小伙伴&#xff0c;可以在…