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))
相关推荐
Seven977 分钟前
SpringCloud 常见面试题(二)
java
讨厌下雨的天空8 分钟前
缓冲区io
linux·服务器·前端
2501_9159090615 分钟前
iOS App 测试工具全景指南,构建从开发、性能到系统级调试的多工具协同测试体系
android·测试工具·ios·小程序·uni-app·iphone·webview
钮钴禄·爱因斯晨21 分钟前
Python常见的文件操作
android·数据库·python
CPU NULL25 分钟前
Redis相关知识点总结
java·数据库·spring boot·redis·缓存
晨晖229 分钟前
Spring Boot整合Spring MVC与外部配置完整笔记
java·spring boot·后端
用户20187928316730 分钟前
希腊字母"Έ"显示不全的奇妙冒险
android
Entropless34 分钟前
Kotlin 可以预判你的预判?Kotlin 高级特性 之 Contracts
android·kotlin
xhxxx42 分钟前
《深入理解 JavaScript 对象:属性命名、访问与遍历的那些细节》
前端·javascript
BBB努力学习程序设计42 分钟前
Web App开发基础知识:从零构建现代化Web应用
前端·html