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

JDBC删除与查询

文章目录

  • 删除操作
  • 查询操作

删除操作

属性配置文件设置
在这里插入图片描述

package com.yanyu;

import java.sql.*;
import java.util.ResourceBundle;

public class DelTest {
    public static void main(String[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle("reswource\\db");
        String driver = bundle.getString("driver");
        String password = bundle.getString("password");
        String user = bundle.getString("user");

        String url = bundle.getString("url");

//        System.out.println(url);


        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName(driver);
//            获取连接对象
            connection = DriverManager.getConnection(url,user,password);
            String sql = "delete from user where name = '烟雨3'";
//            操作对象
            statement = connection.createStatement();

            statement.execute(sql);





        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }




}

查询操作

在这里插入图片描述
在这里插入图片描述

package com.yanyu;

import java.sql.*;
import java.util.ResourceBundle;

public class QueryTest {
    public static void main(String[] args) {

        ResourceBundle bundle = ResourceBundle.getBundle("resource\\db");
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");


        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;


        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,user,password);
            String sql = "select * from user";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);

            while (resultSet.next()){
                String name = resultSet.getString("name");
//                与数据库字段名字一致
                String password1 = resultSet.getString("password");
                System.out.print("姓名:" + name);
                System.out.print("\t\t\t");
                System.out.println("密码:" + password1);


            }
            resultSet.toString();





        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }
}

在这里插入图片描述

相关文章:

  • PLC协议
  • 动态代理模式实现与对比(JDK、CGLIB、Spring AOP)
  • vue数据两个相同的参数对比只显示一个
  • HarmonyOS主题管理工具封装:动态切换、持久化存储与常见问题解析
  • sourcetree中的“master“,“origin/master“,“origin/HEAD“这三个图标都是什么意思?GIT 超详细➕通俗易懂版本
  • Unity中对象池(Object Pool)技术解析与实现
  • 【聚合函数、分组、排序笔记】
  • SSE单向消息推送(get请求)
  • 神经网络 - 前馈神经网络(FNN)、全连接神经网络(FCNN)和卷积神经网络(CNN)的区别与联系
  • MySQL的多表查询
  • 软考《信息系统运行管理员》- 6.1 信息系统安全概述
  • Oracle数据库数据编程SQL<2.2 DDL 视图、序列>
  • lxd-dashboard 图形管理LXD/LXC
  • Processor System Reset IP 核 v5.0(vivado)
  • Allegro界面颜色改变设置
  • Qt应用系统托盘区域显示、托盘菜单示例
  • 快速排序不啦不啦
  • 嵌入式Linux网络编程:UNIX Domain Socket进程间通信(IPC)
  • Maven版本统一管理
  • 如何在Webpack中配置别名路径?
  • 郑培凯:汤显祖的“至情”与罗汝芳的“赤子之心”
  • 美国拟向阿联酋和沙特AI公司出口数十万枚芯片
  • 技术派|更强的带刀侍卫:从054B型战舰谈谈世界护卫舰发展
  • 网信部门曝光网络谣言典型案例,“AI预测彩票号码百分百中奖”等在列
  • 季后赛主场优势消失之谜,这事竟然要赖库里
  • 农行深圳市分行原副行长王国彪涉嫌严重违纪违法被查