java-web JDBC

jdbc

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 

 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))
  public static void t1() {
        String username = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = DriverManager.getConnection(url, username, password);
            String sql = "select  * from t_c2";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            List<Student> studentList = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String pro = resultSet.getString("专业");
                String studentCode = resultSet.getString("学号");
                String classroom = resultSet.getString("班级");
                studentList.add(new Student(id, pro, studentCode, classroom));
            }
            studentList.forEach(System.out::println);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值