JDBC编程 连接数据库【精华】

🏹  封装版附送在文末哦~~ 🏹

目录

0. 导入jar包

1. 设置数据源 DataSource

2. 建立连接 Connection

3. 构造SQL语句 PreparedStatement

(以Update为例)

(以Select为例)

4. 执行SQL语句 executeQuery/Update

(以Update为例)

(以Select为例)ResultSet结果集合

5. 释放资源 .close();

🏹封装版!!(可直接复制使用~)


0. 导入jar包

导入详细方法见:JDBC编程(Java操作数据库 MySQL)

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.*;
import javax.sql.DataSource;
import java.util.Scanner;

1. 设置数据源 DataSource

// 设置数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1230");

2. 建立连接 Connection

// 建立连接
        Connection connection = dataSource.getConnection();

3. 构造SQL语句 PreparedStatement

(以Update为例)

// 构造SQL语句
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的id:");
        int id = scanner.nextInt();
        System.out.println("请输入要修改的名字:");
        String name = scanner.next();

        // 根据功能需要设置sql语句
        String sql = "update student set name = ? where id = ?";

        // 使用 PreparedStatement 构造语句
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);

(以Select为例)

// 构造SQL语句
        String sql = "select * from student";

        // 使用 PreparedStatement 构造语句
        PreparedStatement statement = connection.prepareStatement(sql);

4. 执行SQL语句 executeQuery/Update

executeQuery返回类型是数据表,executeUpdate返回一个整数,表示影响到的行数

(以Update为例)

// 执行SQL语句
        int ret = statement.executeUpdate();
        System.out.println("ret: "+ ret);

(以Select为例)ResultSet结果集合

// 执行SQL语句
        ResultSet resultSet = statement.executeQuery();
        // 遍历结果集合,结果集合非常类似于迭代器
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id: "+id+",name: "+name);
        }

5. 释放资源 .close();

// 释放资源
        // Select操作需要释放结果集合resulSet
        // resultSet.close();
        statement.close();
        connection.close();

🏹封装版!!(可直接复制使用~)

类名:DBUtil.java

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// 通过 DBUtil.java 这个类,把数据库的连接操作封装起来
public class DBUtil {
    private static String url = "jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false";    //默认IP、端口地址
    private static String username = "root";
    private static String password = "1230";    //数据库密码

    // 创建 DataSource 实例
    private static DataSource dataSource = new MysqlDataSource();

    // 静态代码块,执行时机:类加载阶段
    static {
        ((MysqlDataSource)dataSource).setURL(url);
        ((MysqlDataSource)dataSource).setUser(username);
        ((MysqlDataSource)dataSource).setPassword(password);
    }

    // 建立连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    // 释放资源
    public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值