java数据库操作-cnblog

news/2025/1/31 6:10:52/

创建lib目录,填入jar包

选择

image-20241013165606569

libraries添加lib目录

image-20241013165630312

package nb;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class JDBCtest {private static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
//    private static final String url = "jdbc:mysql://localhost:3306/testweb?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";private static final String username = "root";private static final String password = "";public static void main(String[] args) {try {Class.forName("com.mysql.cj.jdbc.Driver");Connection connection = DriverManager.getConnection(url,username,password);if (connection != null){System.out.println("数据库链接成功");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}
}

image-20241013173426532

mysql -h localhost -u root -p

1 实现对数据库的取值

package nb;import java.sql.*;
import java.util.Date;
import java.util.concurrent.locks.StampedLock;public class JDBCtest {private static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";private static final String username = "root";private static final String password = "";public static void main(String[] args) {try {Class.forName("com.mysql.cj.jdbc.Driver");Connection connection = DriverManager.getConnection(url, username, password);if (connection != null) {System.out.println("数据库链接成功");}String sql = "SELECT id,number,name,gender,createDt FROM student";Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()){int id = resultSet.getInt("id");System.out.println("id:"+id);int number = resultSet.getInt("number");System.out.println("number:"+number);String name = resultSet.getString("name");System.out.println("name:"+name);String gender = resultSet.getString("gender");System.out.println("gender:"+gender);Date createDt = resultSet.getDate("createDt");System.out.println("createDt:"+createDt);}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}
}

image-20241013181257085

2 注意在结尾关闭数据库

package nb;import java.sql.*;
import java.util.Date;
import java.util.concurrent.locks.StampedLock;public class JDBCtest {private static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";private static final String username = "root";private static final String password = "";public static void main(String[] args) {try {Class.forName("com.mysql.cj.jdbc.Driver");Connection connection = DriverManager.getConnection(url, username, password);if (connection != null) {System.out.println("数据库链接成功");}String sql = "SELECT id,number,name,gender,createDt FROM student";Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()){int id = resultSet.getInt("id");System.out.println("id:"+id);int number = resultSet.getInt("number");System.out.println("number:"+number);String name = resultSet.getString("name");System.out.println("name:"+name);String gender = resultSet.getString("gender");System.out.println("gender:"+gender);Date createDt = resultSet.getDate("createDt");System.out.println("createDt:"+createDt);}resultSet.close();statement.close();connection.close();System.out.println("关闭数据库完成");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}
}

image-20241013181450093

3 对数据库进行模糊查询

package nb;import java.sql.*;
import java.util.Date;
import java.util.concurrent.locks.StampedLock;public class JDBCtest {private static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";private static final String username = "root";private static final String password = "";public static void main(String[] args) {try {Class.forName("com.mysql.cj.jdbc.Driver");Connection connection = DriverManager.getConnection(url, username, password);if (connection != null) {System.out.println("数据库链接成功");}String sql = "SELECT * FROM student where name like '张%'";Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()){int id = resultSet.getInt("id");System.out.println("id:"+id);int number = resultSet.getInt("number");System.out.println("number:"+number);String name = resultSet.getString("name");System.out.println("name:"+name);String gender = resultSet.getString("gender");System.out.println("gender:"+gender);Date createDt = resultSet.getDate("createDt");System.out.println("createDt:"+createDt);}resultSet.close();statement.close();connection.close();System.out.println("关闭数据库完成");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}
}

查询结果只有一个,关键变换位置在

String sql = “SELECT * FROM student where name like ‘张%’”;

数据库内部一共储存了两个值

image-20241013182117975

image-20241013182126539

4 实现对数据库的增删改查

package nb;import java.sql.*;
import java.util.Date;
import java.util.concurrent.locks.StampedLock;public class JDBCtest {private static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";private static final String username = "root";private static final String password = "";Connection connection;public void init_Connection(){//初始化链接数据库try {Class.forName("com.mysql.cj.jdbc.Driver");connection = DriverManager.getConnection(url, username, password);if (connection != null) {System.out.println("数据库链接成功");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}public void closeConnection(){ //关闭数据库if (connection != null){try {connection.close();System.out.println("数据库成功关闭");}catch (SQLException e){e.printStackTrace();}}}public void showAllData(){try {String sql = "SELECT * FROM student";Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()){int id = resultSet.getInt("id");System.out.println("id:"+id);int number = resultSet.getInt("number");System.out.println("number:"+number);String name = resultSet.getString("name");System.out.println("name:"+name);String gender = resultSet.getString("gender");System.out.println("gender:"+gender);}}catch(SQLException e){e.printStackTrace();}}public void add(int id,int number,String username,String gender){try {String sql = "insert into student values(?,?,?,?)";PreparedStatement statement = connection.prepareStatement(sql);statement.setInt(1,id);statement.setInt(2,number);statement.setString(3,username);statement.setString(4,gender);statement.executeUpdate();System.out.println("添加新同学成功");} catch (SQLException e) {e.printStackTrace();}}public void delete(int id){ //删除指定id的学生try {Statement statement = connection.createStatement();statement.executeUpdate("delete from student where id="+id);System.out.println("删除某成员成功");} catch (SQLException e) {e.printStackTrace();}}public void update(int id,String new_name){String sql = "update student set name = ? where id = ?";try {PreparedStatement preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(2,id);preparedStatement.setString(1,new_name);preparedStatement.executeUpdate();} catch (SQLException e) {e.printStackTrace();}}public static void main(String[] args) {JDBCtest jdbCtest = new JDBCtest();jdbCtest.init_Connection();jdbCtest.add(4,12233,"王二","男");jdbCtest.delete(4);jdbCtest.update(3,"呃呃");jdbCtest.showAllData();jdbCtest.closeConnection();}
}

http://www.ppmy.cn/news/1538781.html

相关文章

adum1201数字隔离器中文资料与应用

ADuM1201是ADI公司推出的一款数字隔离器,其典型应用有工业自动化、通讯电源管理、医疗设备以及汽车等领域。本文将对ADuM1201数字隔离器进行详细的介绍和应用分析,以帮助读者更好地了解和使用该产品。 一、ADuM1201数字隔离器概述 1、基本参数 ADuM120…

MySQL【知识改变命运】05

1&#xff1a;where的基本用法练习&#xff08;比较运算符&#xff09; 基本操作&#xff1a;查询英语<60的同学 如果english为NULL&#xff0c;会自动过滤掉&#xff0c;NULL比较特殊 查询语⽂成绩⾼于英语成绩的同学 这个过程&#xff1a;先读取表中每行记录&#xf…

深度学习——线性神经网络(三、线性回归的简洁实现)

目录 3.1 生成数据集3.2 读取数据集3.3 定义模型3.4 初始化模型参数3.5 定义损失函数3.6 定义优化算法3.7 训练 在上一节中&#xff0c;我们通过张量来自定义式地进行数据存储和线性代数运算&#xff0c;并通过自动微分来计算梯度。实际上&#xff0c;由于数据迭代器、损失函数…

数字媒体技术基础:色度子采样(4:4:4、4:2:2 、4:2:0)

在数字视频处理中&#xff0c;色度子采样 Chroma Subsampling可以用于压缩视频文件的大小&#xff0c;同时在大多数情况下保持较高的视觉质量&#xff0c;它的原理基于人类视觉系统对亮度 Luminance比对色度 Chrominance更加敏感这一特点。 一、 采样格式的表示方法 色度子采样…

软考《信息系统运行管理员》- 4.1信息系统软件运维概述

4.1信息系统软件运维概述 文章目录 4.1信息系统软件运维概述信息系统软件运维的概念信息系统软件的可维护性及维护类型对软件可维护性的度量可以从以下几个方面进行&#xff1a;软件维护分类&#xff1a; 信息系统软件运维的体系1.**需求驱动**2.**运维流程**3.**运维过程**4.*…

uni-app之旅-day05-商品详情

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言7.0 创建 goodsdetail 分支7.2 获取商品详情数据 7.3 渲染商品详情页的 UI 结构7.3.1 渲染轮播图区域7.3.2 实现轮播图预览效果7.3.3 渲染商品信息区域7.3.4 渲…

大数据Lambda 架构和Kappa 架构的优缺点和使用场景

Lambda 架构和Kappa 架构是用于处理大数据的两种架构设计模式&#xff0c;针对不同的数据处理需求提供了不同的方案。它们各自有优缺点&#xff0c;并适用于不同的使用场景。 Lambda 架构 Lambda 架构分为三个层次&#xff1a;批处理层&#xff08;Batch Layer&#xff09;、…

Ubuntu下的CUDA环境的安装与配置

经常要在Ubuntu上配置CUDA的运行环境&#xff0c;这里记录下避免之后忘记。 在 Ubuntu 上配置 CUDA 环境&#xff0c;通常需要以下几个步骤&#xff1a; 检查系统需求和安装前的准备安装 NVIDIA 驱动安装 CUDA 工具包配置环境变量验证 CUDA 安装&#xff08;可选&#xff09;…