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

后端找数据库

一、连接数据库

二、查找  resultSet只使用于查找

 ResultSet resultSet = statement.executeQuery(sql);

//执行sql的对象调用方法执行sql语句 executeQuery查找 返回查询的结果

三、更新 / 删除 / 插入操作(executeUpdate

1.删除

  • 执行修改类 SQL(update/delete/insert 语句),返回受影响的行数(如 delete 语句返回删除了多少条记录)。
    String sql = "delete from student where id = 4";
    int num = statement.executeUpdate(sql); // num 为 1(删除 1 条记录)

2.删除记录

四、修改

五、总体代码

package db.java.copy;public class test {public static void main(String[] args) {}//前段找后端 后端找数据库//查找  resultSet只使用于查找public static String search(String sql) {//加载驱动 trytry {//sql注入Class.forName("com.mysql.jdbc.Driver");//加载 MySQL JDBC 驱动程序;固定写法String url = "jdbc:mysql://localhost:3306/javaweb";//定义数据库url javaweb是表的数据库名字,写法固定String user = "root";//账号密码String password = "root";//驱动管理类调方法进行连接 得到连接对象Connection connection = (Connection) DriverManager.getConnection(url, user, password);//连接对象调用方法 创建执行sql的对象Statement statement = (Statement) connection.createStatement();//执行sql的对象调用方法执行sql语句 executeQuery查找 返回查询的结果ResultSet resultSet = statement.executeQuery(sql);//处理结果String res = "[";while (resultSet.next()) {//从表格中提取数据,存储到相应的变量中int id = resultSet.getInt("id");String name = resultSet.getString("name");String sex = resultSet.getString("sex");int age = resultSet.getInt("age");int classid = resultSet.getInt("classid");System.out.println(id + " " + name + " " + sex + " " + age + " " + classid);res += "{\"id\":\"" + id + "\",\"name\":\"" + name + "\",\"sex\":\"" + sex + "\",\"age\":\"" + age + "\",\"classid\":\"" + classid + "\"},";}//打印出提取的数据,储存到res中res = res.substring(0, res.length() - 1);res += "]";//完成JSON数组创建,[ ]//关闭资源if (resultSet != null) {resultSet.close();}if (statement != null) {statement.close();}if (connection != null) {connection.close();}return res;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return "";}}// 删除public static int delete(String sql) {try {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/javaweb";String user = "root";String password = "root";//使用DriverManager获取与数据库的连接对象。Connection connection = (Connection) DriverManager.getConnection(url, user, password);//通过connection对象创建一个 Statement对象,用于执行 SQL语句。Statement statement = (Statement) connection.createStatement();//通过  statement  对象执行传入的 SQL 更新语句,并返回受影响的行数。int num = statement.executeUpdate(sql);if (statement != null) {statement.close();}if (connection != null) {connection.close();}return num;} catch (Exception e) {e.printStackTrace();return 0;}}//修改public static int update(String sql) {try {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/javaweb";String user = "root";String password = "root";Connection connection = (Connection) DriverManager.getConnection(url, user, password);Statement statement = (Statement) connection.createStatement();int num = statement.executeUpdate(sql);if (statement != null) {statement.close();}if (connection != null) {connection.close();}return num;} catch (Exception e) {e.printStackTrace();return 0;}}public static void main(String[] args) {/*String sql = "select * from class";  //查询 class 表的所有记录 String[] columns = {"id","name"};//调用MysqlUtil.getJsonBySql(sql, columns)方法,  * 将查询结果转换为 JSON格式,并将结果存储在res变量中。String res = MysqlUtil.getJsonBySql(sql, columns);System.out.println(res);*/    String sql = "delete from student where id = 4";//从student表中,删除 id = 4 的记录int num = MysqlUtil.del(sql);//调用 MysqlUtil工具类的del方法,执行上述删除 SQL。System.out.println(num);//直接调方法删除就可以}}}

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

相关文章:

  • nvm install 14.21.3 时npm 无法下载和识别
  • 项目实例-页面
  • 股票智能体系统的设计与开发
  • VisualSVN Server 4.2.1 安装教程 - 64位下载与配置步骤详解
  • 【Docker项目实战】使用Docker部署Vikunja任务管理工具
  • 【C语言强化训练16天】--从基础到进阶的蜕变之旅:Day2
  • 嵌入式 - Linux软件编程:文件IO
  • 零售业CRM实战:如何打通线上线下客户数据?
  • Ansible 基本使用
  • UE官方文档学习 C++ TAarry 查询(三)Contain,Find函数的使用
  • Redis--day4--实战-黑马点评--搭建环境
  • WPS JS宏 通用方法整理汇总 实时更新
  • 【Vue 3 响应式系统深度解析:reactive vs ref 全面对比】
  • MySQL(下)
  • C语言入门完结篇_结构体、枚举、时间函数的、变量类型(C语言划分内存各个区块的方法)、文件操作
  • MyBatis 缓存与 Spring 事务相关笔记
  • Spring-Cache 缓存数据
  • damn the jvm again(2)
  • 编程模型设计空间的决策思路
  • 什么是跨域访问问题,如何解决?
  • Windows怎样配置动态磁盘
  • [SC]SystemC中的SC_FORK和SC_JOIN用法详细介绍
  • android端自定义通话通知
  • VUE的8个生命周期
  • Orange的运维学习日记--41.Ansible基础入门
  • sqli-labs通关笔记-第44关 POST字符型堆叠注入(单引号闭合 手工注入+脚本注入3种方法)
  • demo 英雄热度榜 (条件筛选—高亮切换—列表渲染—日期显示)
  • Full GC 频率优化实战
  • RGWRados::get_obj_state_impl()
  • 25C机场航班调度程序(JS 100)