MySQL database (10)

Table of contents

1. Java database programming: JDBC

1.1JDBC working principle

2. Java specifically connects to the database

2.1 Preparation process

2.2 Code to connect to the database


1. Java database programming: JDBC

JDBC, Java Database Connectivity, java database connection. It is a Java API for executing SQL statements. It is a database connection specification in Java. This API consists of some classes and interfaces in the java.sql.* and javax.sql.* packages, which can provide unified access to multiple relational databases.

1.1JDBC working principle

JDBC access database hierarchy:

JDBC advantages:

  • Java language access database operations are completely oriented to abstract interface programming
  • Developing database applications does not need to be limited to the API of a specific database vendor.
  • Program portability is greatly enhanced

2. Java specifically connects to the database

2.1 Preparation process

First, select the corresponding jar package according to your mysql version. It is recommended to use the central warehouse https://mvnrepository.com/ to download the corresponding jar package.

If your mysql version is 5. or more, just download one of the corresponding 5 versions. For 8. versions, just download one of the 8. versions. You cannot use the 8 version jar package for the 5 version.

 Then start building the project and create a lib folder in the project

Copy the downloaded jar package to the lib folder, then right-click the lib folder, find and click Add, and finally click OK. The preparations are over.

2.2 Code to connect to the database

1. Create a data source:

DataSource dataSource = new MysqlDataSource();
//这个setUrl中的直接复制就行了,不需要刻意的去记,但是一点要更改这个test数据库为自己的数据库
((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
//输入用户名,一般都是root
((MysqlDataSource) dataSource).setUser("root");
//输入自己的数据库密码,
((MysqlDataSource) dataSource).setPassword("123456");

2. Establish a connection with the database

//这里会有一个异常,直接抛出去就行了
Connection connection = dataSource.getConnection();

3. Construct sql statement 

There is already a test table by default. If you don't have one, you can create one yourself or use your own table.

create table test(

        id int,

        name varchar(20)

);

String sql = "insert into test values(1,'张三')";
//使用prepareStatement对sql语句进行解析
PreparedStatement statement = connection.prepareStatement(sql);

4. Execute sql statement

int n = statement.executeUpdate();

Overall process:

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

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

public class JDBCDemo2 {
    public static void main(String[] args) throws SQLException {
        //1.创建“数据源”
        DataSource dataSource = new MysqlDataSource();
        //这个setUrl中的直接复制就行了,不需要刻意的去记,但是一定要更改这个test数据库为自己的数据库
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        //输入用户名,一般都是root
        ((MysqlDataSource) dataSource).setUser("root");
        //输入自己的数据库密码,
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.和数据库服务器建立连接
        //这里会有一个异常,直接抛出去就行了
        Connection connection = dataSource.getConnection();

        //3.构造sql语句,这里默认已经有了test表
        String sql = "insert into test values(1,'张三')";
        //使用prepareStatement对sql语句进行解析
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行sql语句
        int n = statement.executeUpdate();

        //释放资源.关闭连接
        statement.close();
        connection.close();
    }
}

To prepare for execution, first check whether the test table contains data. It is found that there is no data. Then start the program and query again to find that there is data, which proves that the database connection is successful!

Guess you like

Origin blog.csdn.net/x2656271356/article/details/131901204