当前位置: 首页 > news >正文

JavaWeb前瞻—JDBC

JDBC

Java Database Connection java数据库连接。一套接口
面向接口编程。

四大参数:

			//检查驱动是否存在Class.forName("com.mysql.cj.jdbc.Driver");//2.创建数据库连接String url = "jdbc:mysql://localhost:3306/students2025";//数据库连接字符串String username ="root";String password = "123456";

三种:
一.读操作
有sql注入风险

public class Test01 {public static void main(String[] args) {//查询操作Connection conn =null;try {//检查驱动是否存在Class.forName("com.mysql.cj.jdbc.Driver");//2.创建数据库连接String url = "jdbc:mysql://localhost:3306/students2025";//数据库连接字符串String username ="root";String password = "123456";conn = DriverManager.getConnection(url,username,password);//3.创建数据库执行器Statement stmt = conn.createStatement();//4.发送sql,执行sqlString sql = "select id,stu_id,name,pinyin,sex,birthday,height,weight from t_student";//5.返回结果集ResultSet rs = stmt.executeQuery(sql);//6 从返回的结果集中获取想要的数据while (rs.next()){Integer id = rs.getInt("id");String stuId = rs.getString("stu_id");String name = rs.getString("name");String pinyin = rs.getString("pinyin");String sex = rs.getString("sex");Date birthday = rs.getDate("birthday");Integer height = rs.getInt("height");BigDecimal weight = rs.getBigDecimal("weight");System.out.println(id+" "+stuId+" "+name+" "+pinyin+" "+sex+" "+birthday+" "+height+" "+weight);}} catch (ClassNotFoundException e) {System.out.println("mysql驱动不存在");} catch (SQLException e) {System.out.println("获取数据库连接失败");}finally {//关闭数据库连接if(conn!=null){try {conn.close();} catch (SQLException e) {throw new RuntimeException(e);}}}}
}

二.读操作
PreparedStatement ps = conn.prepareStatement(sql);
避免sql注入

public class Test02 {public static void main(String[] args) {String url = "jdbc:mysql://localhost:3306/students2025";String user = "root";String password ="123456";try {Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {System.out.println("11111");}//try()里的代码,相当于在finally里关闭----自动关闭try(Connection conn = DriverManager.getConnection(url,user,password);) {String sql ="select id,stu_id,name,sex,birthday,weight from t_student where weight>? limit ?,?";//避免sql注入PreparedStatement ps = conn.prepareStatement(sql);ps.setFloat(1,50.5f);ps.setInt(2,10);ps.setInt(3,5);//无需再指定sqlResultSet rs =ps.executeQuery();while (rs.next()){int id = rs.getInt("id");String stuId = rs.getString("stu_id");String sex = rs.getString("sex");Date birthday = rs.getDate("birthday");System.out.println(id+stuId+sex+birthday);}} catch (Exception e) {System.out.println("11111");}}
}

三.写操作

public class Test03 {public static void main(String[] args) {try {Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {throw new RuntimeException(e);}String url = "jdbc:mysql://localhost:3306/students2025";String user = "root";String password ="123456";try (Connection conn = DriverManager.getConnection(url,user,password);){//insert into t_student (id,stu_id)values(?,?)String sql = "DELETE  from t_student where height >? and weight >? ";PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1,180);ps.setBigDecimal(2, BigDecimal.valueOf(90));//写操作:增删改都是excutUpdateint rows = ps.executeUpdate();System.out.println(rows);} catch (SQLException e) {throw new RuntimeException(e);}}
}

尚待更新…2025-7-30

http://www.dtcms.com/a/306406.html

相关文章:

  • Rabbitmq中常见7种模式介绍
  • QString 内存机制详解
  • 【Excel】制作双重饼图
  • 恢复IP地址
  • 明远智睿V2H核心模组:工业4.0时代的“性价比革命”
  • 双塔模型 + 自监督学习:解决长尾物品表征难题
  • IBus vs. Fcitx5:一场 Linux 输入法框架的正面交锋
  • Maximum Subarray Sum
  • Redis高可用性
  • CSM7020L 磷酸铁锂电池充电管理的太阳能草坪灯 LED 驱动芯片 SOT23-6封装 带多种反接功能
  • LLM之RAG理论(十八)| ChatGPT DeepResearch 深度研究功能全面技术分析报告
  • 使用Docker 在Rocky Linux 9.5上在线安装Dify
  • 2025年DDoS攻防战:六层防护体系构建业务“数字免疫”
  • 从0开始学linux韦东山教程Linux驱动入门实验班(7)
  • 伦敦招聘数据管道系统设计与实现
  • android-PMS-常见定制场景
  • 【文章浏览 I】
  • 【7】串口编程三种模式(查询/中断/DMA)韦东山老师学习笔记(课程听不懂的话试着来看看我的学习笔记吧)
  • luoguP13511 [KOI P13511 [KOI 2025 #1] 等腰直角三角形
  • S3、SFTP、FTP、FTPS 协议的概念、对比与应用场景
  • vulhub ica1靶场攻略
  • AI框架工具FastRTC快速上手2——整体框架及Stream类详解
  • 浏览器pdf、image显示
  • MaxKB+MinerU:通过API实现PDF文档解析并存储至知识库
  • 虚幻基础:旋转体
  • 在java开发中,错误信息类中定义一个errMap,为什么要在static{}中,put键值对?这是为什么?好处是什么?
  • 嵌入式 C 语言入门:分支结构(if/switch)的用法与硬件控制实践
  • [ java IO ] 文件传输中的输入输出(流)
  • 算法能力提升之快速矩阵
  • PSO-TCN-BiLSTM-MATT粒子群优化算法优化时间卷积神经网络-双向长短期记忆神经网络融合多头注意力机制多特征分类预测/故障诊断Matlab实现