java查询添加修改删除jdbc实测代码

[复制链接]
admin 发表于 2025-8-20 13:40:23 | 显示全部楼层 |阅读模式
java查询添加修改删除jdbc实测代码
添加msyql的jar包
  1. package com.jinhei.fruit.dao.base;


  2. import java.sql.*;

  3. public class JdbcQuick {
  4.     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  5.         //1.注册驱动
  6.       //  Class.forName("com.mysql.cj.jdbc.Driver");

  7.         //2.获取数据库连接
  8.         Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/fruitdb", "root", "root");

  9.         //3.创建Statement对象
  10.       //  PreparedStatement preparedStatement = connection.prepareStatement("select * from t_fruit");

  11.         //4.编写SQL语句并执行,获取结果
  12.       //  ResultSet resultSet = preparedStatement.executeQuery();

  13.         //3.创建Statement对象
  14.         PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `t_fruit` (`fname`, `price`, `fcount`, `remark`)values  (?,?,?,?)");

  15.         //4.为占位符赋值,索引从1开始,编写SQL语句并执行,获取结果
  16.         preparedStatement.setString(1,"西瓜");
  17.         preparedStatement.setDouble(2,43.23);
  18.         preparedStatement.setInt(3,28);
  19.         preparedStatement.setString(4,"好甜");
  20.         int result = preparedStatement.executeUpdate();

  21.         //5.处理结果
  22.         if(result>0){
  23.             System.out.println("添加成功");
  24.         }else{
  25.             System.out.println("添加失败");
  26.         }

  27.         //6.释放资源(先开后关原则)
  28.         preparedStatement.close();
  29.         connection.close();

  30.         //5.处理结果
  31. //        while (resultSet.next()) {
  32. //            int fId = resultSet.getInt("fid");
  33. //            System.out.println("fId = " + fId);
  34. //        }
  35.     }
  36. }
复制代码
网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:17817817816
微信扫码

网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等

粤ICP备2024252464号

在本版发帖
微信扫码
QQ客服返回顶部
快速回复 返回顶部 返回列表