Jdbc初测试(一)

这篇博客介绍了JDBC的工作原理,它作为接口由各数据库厂商实现。内容包括JDBC核心API,如Connection和ResultSet接口,以及使用JDBC连接MySQL数据库的详细步骤,包括加载驱动、创建连接、预处理SQL、执行查询和处理结果集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、JDBC的工作原理

JDBC仅为一个接口(规则),由各个数据库厂商进行实现(即JDBC驱动)

二、JDBC 核心API简介

JDBC API的主要功能:与数据库建立连接、执行SQL 语句、处理结果和释放资源
在这里插入图片描述

三、JDBC的开发步骤

  1. 加载驱动 Class.forName()

  2. 链接数据库 Connection

  3. 预处理SQL语句 PreparedStatement

  4. 执行,并返回结果 resultSet

  5. 释放资源 close()

    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
    	//1.加载驱动
    	Class.forName(“JDBC驱动类”); 
    	//2.获取Connection 连接对象
    	con=DriverManager.getConnection(URL,数据库用户名,密码);
    	//3.创建Statement对象,执行SQL语句
    	stmt = con.createStatement();
    	//4.返回ResultSet并查询结果
    	rs = stmt.executeQuery(“SELECT a, b, c FROM table1;”);
    	while (rs.next()) {
    		//每次读取一行,并打印读取结果
    	}
    }catch(Exception e){ 
    	e.printStackTrace();
    }finally{
    		//5.释放资源
    		try {if(rs != null) rs.close();
    		if(stmt != null) stmt.close();
    		if(con != null) con.close();
    		} catch(SQLException e) {
    			//异常处理
    	}
    }
    
    
Connection接口的常用方法

在这里插入图片描述

ResultSet接口的常用方法

在这里插入图片描述

[

例子:

package com.school;

import java.sql.*;

/**
 * @authorDesc 
 * @author 
 * @date 2022-09-06 15:55:49
 * @version
 * @description 数据库工具类
 */
public class MysqlUtil {
    /**
     * 数据库链接地址
     */
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/MYSCHOOL";
    /**
     * 数据库用户名
     */
    private static final String USERNAME= "root";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "123456";
    /**
     * 数据库链接
     */
    private static Connection connection = null;
    /**
     * 数据库语句执行
     */
    private static Statement statement = null;
    /**
     * 数据库查询结果
     */
    private static ResultSet resultSet = null;
    /**
     * @description 数据库建立链接
     * @author 
     * @date 2022-09-06 16:14:10
     * @param
     * @return
     */
    public static void start() throws ClassNotFoundException, SQLException {
        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 链接数据库
        connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
    }
    /**
     * @description 数据库查询语句
     * @author
     * @date 2022-09-06 16:17:25
     * @param sql 数据库查询语句
     * @return {@link ResultSet} 查询结果
     */
    public static ResultSet select(String sql) throws SQLException {
        // 发送SQL语句并执行
        statement = connection.createStatement();
        // 返回结果
        resultSet = statement.executeQuery(sql);
        return resultSet;
    }
    /**
     * @description 关闭数据库
     * @author 
     * @date 2022-09-06 16:14:46
     * @param
     * @return
     */
    public static void close(){
        try {
            if (resultSet != null){
                resultSet.close();
                resultSet = null;
            }
            if (statement != null){
                statement.close();
                statement = null;
            }
            if (connection != null){
                connection.close();
                connection = null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
package com.school;

import java.sql.*;

/**
 * @authorDesc 
 * @author 
 * @date 2022-09-06 15:55:49
 * @version
 * @description 数据库工具类
 */
public class MysqlUtil {
    /**
     * 数据库链接地址
     */
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/MYSCHOOL";
    /**
     * 数据库用户名
     */
    private static final String USERNAME= "root";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "123456";
    /**
     * 数据库链接
     */
    private static Connection connection = null;
    /**
     * 数据库语句执行
     */
    private static Statement statement = null;
    /**
     * 数据库查询结果
     */
    private static ResultSet resultSet = null;
    /**
     * @description 数据库建立链接
     * @author 
     * @date 2022-09-06 16:14:10
     * @param
     * @return
     */
    public static void start() throws ClassNotFoundException, SQLException {
        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 链接数据库
        connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
    }
    /**
     * @description 数据库查询语句
     * @author 
     * @date 2022-09-06 16:17:25
     * @param sql 数据库查询语句
     * @return {@link ResultSet} 查询结果
     */
    public static ResultSet select(String sql) throws SQLException {
        // 发送SQL语句并执行
        statement = connection.createStatement();
        // 返回结果
        resultSet = statement.executeQuery(sql);
        return resultSet;
    }
    /**
     * @description 关闭数据库
     * @author 
     * @date 2022-09-06 16:14:46
     * @param
     * @return
     */
    public static void close(){
        try {
            if (resultSet != null){
                resultSet.close();
                resultSet = null;
            }
            if (statement != null){
                statement.close();
                statement = null;
            }
            if (connection != null){
                connection.close();
                connection = null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值