java-web JDBC

jdbc

java 复制代码
package com.gyk.jdbc;


import com.mysql.jdbc.Driver;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * ClassName: D
 * Package: com.gyk.jdbc
 * Description:
 *
 * @Author Samuel
 * @Create 2024/8/1 13:05
 * @Version 1.0
 */
public class D {
    @Test
    public void m1() throws ClassNotFoundException, SQLException {
        //连接本地 mysql
        //获取驱动类
//        Class.forName("com.mysql.jdbc.Driver");
        //url
        String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
        //username password
        String username = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println(connection);
//        DriverManager

    }
}
复制代码
/**
 * 在 JDBC 中,PreparedStatement.execute() 方法返回一个布尔值,
 * 表示执行的 SQL 语句是否返回了一个 ResultSet 对象。
 * 对于 INSERT、UPDATE 和 DELETE 语句,execute() 方法通常返回 false,
 * 因为这些语句不会返回 ResultSet 对象。
 */
复制代码
executeUpdate

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Returns:

either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

Throws:

SQLException -- if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement returns a ResultSet object

SQLTimeoutException -- when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement

java 复制代码
 Boolean aBoolean = preparedStatement.execute();

Returns:
true if the first result is a ResultSet object; 
false if the first result is an update count or there is no result


select * 得到 true ( 15 rows in set (0.00 sec))

insert update delete 得到false(Query OK, 1 row affected (0.00 sec))
相关推荐
疯狂踩坑人4 分钟前
【React 19 尝鲜】第一篇:use和useActionState
前端·react.js
X***07885 分钟前
从语言演进到工程实践全面解析C++在现代软件开发中的设计思想性能优势与长期生命力
java·开发语言
毕设源码-邱学长7 分钟前
【开题答辩全过程】以 基于VUE的打车系统的设计与实现为例,包含答辩的问题和答案
前端·javascript·vue.js
用户390513321928810 分钟前
JS判断空值只知道“||”?不如来试试这个操作符
前端·javascript
smileNicky10 分钟前
SpringBoot系列之集成Pulsar教程
java·spring boot·后端
海云前端110 分钟前
前端面试必问 asyncawait 到底要不要加 trycatch 90% 人踩坑 求职加分技巧揭秘
前端
Sammyyyyy30 分钟前
Rust 1.92.0 发布:Never Type 进一步稳定
java·算法·rust
alonewolf_9941 分钟前
深入解析G1与ZGC垃圾收集器:原理、调优与选型指南
java·jvm·算法
小镇学者42 分钟前
【c++】C++字符串删除末尾字符的三种实现方法
java·开发语言·c++
rfidunion43 分钟前
springboot+VUE+部署(1。新建项目)
java·vue.js·spring boot