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

Java 手写jdbc访问数据库

springboot流行起来后,数据库的操作都由springboot自动装配了,好处是方便,但是有时候我们只需要写一个简单的脚本去访问数据库,相信很多人都不会了!

以下给出具体实现

建立链接

    public static void main(String[] args) {Connection conn = null;Statement stmt = null;try {String URL = "jdbc:mysql://127.0.0.1:3306/test";String USER = "root";String PASSWORD = "123456";// 获取数据库连接conn = DriverManager.getConnection(URL, USER, PASSWORD);//todo  } catch (SQLException e) {e.printStackTrace();} finally {// 关闭Statement和Connectiontry {if (stmt != null) stmt.close();if (conn != null) conn.close();} catch (SQLException e) {e.printStackTrace();}}}

执行固定sql查询

    public static void main(String[] args) {Connection conn = null;Statement stmt = null;try {String URL = "jdbc:mysql://127.0.0.1:3306/test";String USER = "root";String PASSWORD = "123456";// 获取数据库连接conn = DriverManager.getConnection(URL, USER, PASSWORD);// 创建Statement对象stmt = conn.createStatement();// 查询数据String sql = "select * from (" +"select " +"TABLE_SCHEMA as `Database`," +"TABLE_NAME as `Table`," +"GROUP_CONCAT(COLUMN_NAME order by SEQ_IN_INDEX separator ', ') as `Columns`," +"INDEX_NAME as `Index`," +"INDEX_TYPE," +"NON_UNIQUE = 0 as `Is Unique` " +"from information_schema.STATISTICS " +"where INDEX_TYPE = 'BTREE' and NON_UNIQUE = 0 " +"group by TABLE_SCHEMA, TABLE_NAME, INDEX_NAME" +") t " +"where `Index` <> 'PRIMARY' and `Database` = 'test'";java.sql.ResultSet rs = stmt.executeQuery(sql);// 处理查询结果while (rs.next()) {String tableName = rs.getString("Table");String uniqueKeyColumn = rs.getString("Columns");String uniqueKeyName = rs.getString("Index");}} catch (SQLException e) {e.printStackTrace();} finally {// 关闭Statement和Connectiontry {if (stmt != null) stmt.close();if (conn != null) conn.close();} catch (SQLException e) {e.printStackTrace();}}}

执行可变条件查询

PreparedStatement preparedStatement = conn.prepareStatement("  SELECT * " +"                        FROM INFORMATION_SCHEMA.COLUMNS " +"                WHERE TABLE_SCHEMA = ? " +"                AND TABLE_NAME = ? " +"                AND COLUMN_NAME = ?;");preparedStatement.setString(1, "test");preparedStatement.setString(2, tableName);preparedStatement.setString(3, "tenant_id");ResultSet resultSet = preparedStatement.executeQuery();

http://www.xdnf.cn/news/235423.html

相关文章:

  • 代理脚本——爬虫
  • 【MySQL】索引特性
  • JGQ511机械振打袋式除尘器实验台装置设备
  • 鸿蒙的StorageLink
  • BT137-ASEMI机器人功率器件专用BT137
  • 【Hive入门】Hive性能优化:执行计划分析EXPLAIN命令的使用
  • 41 python http之requests 库
  • spring中的@Configuration注解详解
  • pytorch的cuda版本依据nvcc --version与nvidia-smi
  • 企业架构之旅(4):TOGAF ADM 中业务架构——企业数字化转型的 “骨架”
  • 永磁同步电机控制算法--单矢量模型预测电流控制MPCC
  • # 实现中文情感分析:基于TextRNN的模型部署与应用
  • 软件测试52讲学习分享:深入理解单元测试
  • BI平台是什么意思?一文讲清BI平台的具体应用!
  • AWTK:一键切换皮肤,打造个性化UI
  • 开源版禅道本地安装卸载备份迁移小白教程
  • Windows服务器部署全攻略:Flask+Vue+MySQL跨平台项目实战(pymysql版)
  • SQL Server 数据库重命名
  • VS2017 编译QCefView
  • 《系统分析师-第三阶段—总结(八)》
  • 从误删到精准识别:300+店铺验证的Vine订单三维判定模型
  • OpenCV实战教程 第一部分:基础入门
  • 【Unity笔记】基于距离驱动的参数映射器 InverseDistanceMapper 设计与实现
  • Modbus转Profibus:一键连接,轻松保护电机!
  • 从错误思路到滑动窗口:力扣2962“包含至少K个最大值”的子数组计数问题---left的解读
  • 经典算法 独立任务最优调度问题
  • Gradio全解20——Streaming:流式传输的多媒体应用(2)——构建对话式聊天机器人
  • 企业微信jdk 授权 记录
  • 蛋白质数据库InterPro介绍
  • 垒球世界纪录多少米·棒球1号位