Java——JDBC

1. JDBC连接MySQL数据库

1.1 操作流程

1. 导入Jar包
	在项目根目录创建lib目录,放入对应jar包,引入依赖
2. 加载驱动
	Java程序只是规定了接口规范,但是没有实现
	数据库连接需要使用JDBC对应驱动
3. 准备必要参数连接数据库
4. 获取数据库连接
5. 关闭资源

1.2 数据库连接演示代码

package com.qfedu;

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

/**
 * @Description TODO
 * @Author 刘珈玮
 * @Date 2020/3/23 15:58
 */
public class Demo1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        /*
        1. 加载驱动
         */
        Class.forName("com.mysql.jdbc.Driver");

        /*
        2. 准备必要的数据
         */
        String url = "jdbc:mysql://localhost:3306/demo1?useSSL=true";
        String user = "root";
        String password = "0920";
        /*
         3. 获取数据库连接
         */
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        /*
        4. 关闭资源
         */
        connection.close();
    }
}

1.3 数据库驱动加载过程

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    //
    // Register ourselves with the DriverManager
    static {
        try {
            // DriverManager驱动管理器注册了当前com.mysql.jdbc.Driver
            // 相对于当前Java程序拥有了连接MySQL数据库的必要的驱动条件
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

    /**
     * Construct a new driver and register it with DriverManager
     * 
     * @throws SQLException
     *             if a database error occurs.
     */
    public Driver() throws SQLException {
        // Required for Class.forName().newInstance()
    }
}

2. JDBC核心API

class java.sql.DriverManager 驱动管理类
--| static java.sql.Connection getConnection(String url, String user, String password);
/*
	这里是根据听的数据库连接URL,对应的user用户名和password密码,获取数据库连接对象
*/

interface java.sql.Connection 数据库连接接口
--| java.sql.Statement createStatement();
/*
	获取数据库SQL语句搬运工对象,从Java程序搬运SQL语句到数据库中,同时Statement也是一个资源对象。
*/
--| java.sql.PreparedStatement prepareStatement(String sql);
/*
	获取数据库SQL语句【预处理】搬运工对象,Java程序的SQL语句,在创建PreparedStatement对象时,将SQL语句交给数据库预处理操作,可以解决一定的【SQL语句注入问题】,同时提高一定的效率,PreparedStatement也是一个资源对象
*/

interface java.sql.Statement 数据库SQL语句搬运工对象
--| int executeUpdate(String sql);
/*
	执行数据库修改数据,insert,update,delete...,返回值类型是int类型,是当前SQL语句搬运到数据库执行之后,数据库运行对于当前操作受到影响的行数
	2 rows affected in 5 ms
*/
--| java.sql.ResultSet executeQuery(String sql);
/*
	执行数据库查询语句,select操作,执行的结果是一个java.sql.ResultSet,结果集对象,当前操作返回值never null
*/

interface java.sql.PreparedStatement 数据库SQL语句【预处理】搬运工对象
    PreparedStatement extends java.sql.Statement
--| int executeUpdate();
/*
	执行数据库修改操作,insert,update,delete...处理的SQL语句是在创建PreparedStatement对象过程预处理的SQL语句,并且返回值是int类型, 为当前操作对于数据表中收到影响的行数
*/
--| java.sql.ResultSet executeQuery();
/*
	执行数据库查询语句,select操作,的SQL语句是在创建PreparedStatement对象过程预处理的SQL语句,执行的结果是一个java.sql.ResultSet,结果集对象,当前操作返回值never null
*/
--| setXXX(int index, XXX value)
/*
	PreparedStatement预处理的SQL语句是可以带有参数的,这里是对于SQL语句参数进行赋值操作,这里有指定的操作下标,和对应的数据,数据类型繁多
*/
    
interface java.sql.ResultSet 数据库结果集接口
--|XXX getXXX(int columnIndex);
/*
	根据查询结果中,字段所处的位置下标获取对应数据,XXX是指定类型
*/
--|XXX getXXX(String fieldName);
/*
	根据查询结果中,字段所处的字段名获取对应数据,XXX是指定类型
*/
--| boolean next();
/*
	判断当前查询结果集中是否还有可以键遍历操作的数据,如果没有。或则当前结果集中是无数据情况 Empty Set,直接返回fasle
*/

3. Statement操作SQL语句

3.1 Statement插入SQL数据操作

@Test
public void testInsert() {
    Statement statement = null;
    Connection connection = null;
    try {
        // 1. 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2. 准备必要的连接数据
        String url = "jdbc:mysql://localhost:3306/demo1?useSSL=true";
        String user = "root";
        String password = "0920";
        //3. 获取数据库连接
        connection = DriverManager.getConnection(url, user, password);
        // 4. 获取Statement对象
        statement = connection.createStatement();
        // 5. 准备SQL语句
        String sql = "insert into nzgp2001.user(userName, password) value ('林博', '1234')";
        // 6. 执行SQL语句
        int affectedRows = statement.executeUpdate(sql);
        System.out.println("affectedRows:" + affectedRows);
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    } finally {
        // 7. 关闭资源
        try {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

3.2 Statement修改SQL数据操作

@Test
public void testUpdate() {
    Statement statement = null;
    Connection connection = null;
    try {
        // 1. 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2. 准备必要的连接数据
        String url = "jdbc:mysql://localhost:3306/demo1?useSSL=true";
        String user = "root";
        String password = "0920";
        //3. 获取数据库连接
        connection = DriverManager.getConnection(url, user, password);
        // 4. 获取Statement对象
        statement = connection.createStatement();
        // 5. 准备SQL语句
        String sql = "update demo1.student set userName ='ljw' where id = 1";
        // 6. 执行SQL语句
        int affectedRows = statement.executeUpdate(sql);
        System.out.println("affectedRows:" + affectedRows);
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    } finally {
        // 7. 关闭资源
        try {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

3.3 Statement删除SQL数据操作

@Test
public void testDelete() {
    Statement statement = null;
    Connection connection = null;
    try {
        // 1. 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2. 准备必要的连接数据
        String url = "jdbc:mysql://localhost:3306/demo1?useSSL=true";
        String user = "root";
        String password = "0920";
        //3. 获取数据库连接
        connection = DriverManager.getConnection(url, user, password);
        // 4. 获取Statement对象
        statement = connection.createStatement();
        // 5. 准备SQL语句
        String sql = "delete from student where id > 2";
        // 6. 执行SQL语句
        int affectedRows = statement.executeUpdate(sql);
        System.out.println("affectedRows:" + affectedRows);
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    } finally {
        // 7. 关闭资源
        try {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
播报频率和波形参数。我们可以使用百度语音合成API来实现语音合成,该API使用 JDBC 连接 Access 数据库,需要先下载 Microsoft 的 JDBC-ODBC 驱动,然后进行以下步骤: 1.可以将文本转换为语音,并以MP3格式进行下载。我们可以使用以下代码来实现语音合成 安装 Access 数据库并创建一个数据库文件(后缀为 .mdb 或 .accdb)。 2. 下载并安装: ```python import urllib.request import json def text_to_speech(text, filename): api_key = 'Your API Key Microsoft Access Database Engine。 3. 在系统的 ODBC 数据源管理器中添加一个数据源,选择 Microsoft Access Driver (*.mdb, *.accdb)。 4. 按照提示填写数据源名称和数据库文件路径等信息。 5. 在 Java 代码中使用 JDBC 连接 Access 数据库,示例代码如下: ```java import java.sql.*; public class AccessJDBC { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 加载 JDBC 驱动 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 连接数据库 String url = "jdbc:odbc:myAccessDB"; String user = ""; String password = ""; conn = DriverManager.getConnection(url, user, password); // 执行 SQL 查询 String sql = "SELECT * FROM myTable"; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); // 处理查询结果 while (rs.next()) { System.out.println(rs.getString("id") + ", " + rs.getString("name")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 其中,url 参数的值应该与 ODBC 数据源管理器中设置的数据源名称相同。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值