current position:Home>JDBC database connection

JDBC database connection

2022-08-06 09:33:09Bugman。

一、JDBC概念

Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法.通俗易懂说:jdbc就是java操作数据库

jdbc的本质就是:javaThe official provides a standard interface,Designed to help programmers developers operating different relational database(mysql/Oracle/SQLServer),jdbcEncapsulates a series of add and delete operation,Database vendors encapsulation realizedjdbcTo the bottom of various database details.

二、JDBC连接数据库

1.创建表

2.在javaThe official just provideJDBC规范的接口,If you need to connect to a specific database 例如

mysql ,我们就需要导入mysql的依赖jar包,Specific implementation is a different database

producers.

1.导入mysql驱动jar包;

下载完后,直接创建一个libs文件夹,把jarPackage copy paste

 

 

右键jar包,点击Add as LibraryCan make the project hasmysql的驱动jar包

 

2.注册驱动 javase 反射机制Class.forName()

3.获取数据库连接

4.获取执行者对象

5.执行sqlStatement and get the return result

6.对结果进行处理

7.释放jdbc资源

jdbcDownload drivers rely on:mysql依赖jar包

package com.lkw.JDBC;
import java.sql.*;
public class Demo01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.导入我们的mysql驱动jar包(已完成)
        //2.注册mysql驱动jar包
        Class.forName("com.mysql.jdbc.Driver");      //Reflection mechanism applied in:jdbc注册驱动/IoC容器
        //3.创建数据库连接  协议 http协议、tcp协议、jdbc协议
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/kaiwen?serverTimezone=UTC", "root", "");
        //4.获取执行者对象
        Statement statement = connection.createStatement();
        //5.Perform the developers to write your ownsql语句
        ResultSet resultSet = statement.executeQuery("select * from customers"); //执行查询sql语句
        //6.执行sqlThe result of the statement processing operations
        while (resultSet.next()) {   //If you have the next row data, take out the bank,如果没有,则返回false
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String phone = resultSet.getString("phone");
            System.out.println("id:" + id + "name:" + name + "phone:" + phone);
        }
        //7.Off these resources
        connection.close();
        statement.close();
    }
}

常见问题

1.Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

原因是没有引入mysql 驱动jar包

2. No suitable driver found for jdbc:mysql//127.0.0.1:3306/mayikt1?serverTimezone=UTC

原因jdbcAddress fill out is wrong

3. Access denied for user 'root1'@'localhost' (using password: YES)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.jav

mysql Connection account and password 错误

4. Table 'mayikt.mayikt_users11' doesn't exist 在我们的mayikt数据库

是没有mayikt_users11

JDBC API 详解

DriverManager 驱动程序管理器是负责管理驱动程序的,驱动注册以后,会保存在DriverManagerIn the list of registered subsequent processing can operate it on the list.

Registration drive mode

1.DriverManager.registerDriver();

2.写代码实现

Class.forName("com.mysql.jdbc.Driver");

3.com.mysql.jdbc.DriverClass of static code faster

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    //
    // Register ourselves with the DriverManager
    //
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

4.Developers are not need to callDriverManager.registerDriver();方法,因为我们在使用class.forName Loads to us

com.mysql.jdbc.Driver 通过Driver静态代码快 注册我们的Driver驱动.

5.mysql5之后,在jar包中存在一个java.sql.Driver配置文件,File specified loadingcom.mysql.cj.jdbc.Driver

通过SPI机制实现.

类加载器、spi 反射技术 javase进阶基础

6.获取连接Connection 连接对象

Connection connection = DriverManager.getConnection("数据库连接地址",
"用户名称", "用户密码");

参数:指定连接的路径 语法:jdbc://mysql://ip地址:端口号码/数据库名称

user:用户名称

pwd:用户的密码

ResultSet结果集对象

1.判断结果集是否有数据: boolean next();

1.1.有数据返回true 并将索引向下移动一行

1.2.没有数据返回false

2.获取结果集中的数据:xxx.getxx(列名称) 注意与 The database data types need corresponding

copyright notice
author[Bugman。],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060927152621.html

Random recommended