JDBC Quick Start: From environment construction to code writing, you can easily implement database addition, deletion, modification and query operations!

After having a certain understanding of the basic concepts and working principles of JDBC, in this article we will explore how to start from scratch, build a development environment step by step, write code, and finally implement the addition, deletion, modification, and query operations of the database.

1. Development environment setup
First, we need to prepare the development environment: Java Development Kit (JDK), database (such as MySQL), and database driver (such as MySQL Connector/J).

Install JDK:
You can download the JDK version suitable for your operating system from Oracle's official website and install it according to the prompts. I believe everyone has already installed this, so I won’t say more here.

Install the database:
Also download the MySQL installation package from the official website and follow the prompts to install it. After the installation is complete, you need to create a database and tables for subsequent testing.

Download the database driver:
Download the corresponding version of MySQL Connector/J from the MySQL official website, and add the decompressed jar file to your project class path.

The specific operations are as follows:
1. Create an ordinary empty project,
Insert image description here
fill in the project name and path
Insert image description here
2. Configure the JDK version
Insert image description here
3. Create a sub-module (the jdbc quick start program is written here)
Insert image description here
fill in the sub-module name here
Insert image description here
and then take the next step. , click OK, this sub-module is created.
Insert image description here
4. Import the jar package
Insert image description here
Insert image description here

2. Use JDBC to access the database.
The steps for JDBC to operate the database are as follows:

Register the driver
, obtain the database connection object (Connection),
define the SQL statement,
obtain the object that executes SQL (Statement),
execute the SQL
processing set and return the result (ResultSet), and
release resources
. Let’s learn about the writing steps and operation process of JDBC code through the code.

1. Create database and tables:

CREATE DATABASE `jdbc_test` DEFAULT CHARSET utf8mb4;
CREATE TABLE `account`(
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'ID', 
`name` varchar(20) NOT NULL COMMENT '姓名',
`salary` int(11)  COMMENT '薪资',
);

2. Write a Java program:

package com.baidou.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBCDemo {
    
    
   public static void main(String[] args) throws Exception {
    
    
   // 1、注册驱动
   Class.forName("com.mysql.jdbc.Driver");
   
   // 2、获取连接
   String url ="jdbc:mysql://127.0.0.1:3306/jdbc_test?useSSL=false";
   String user = "root";
   String password = "123456";
   Connection conn = DriverManager.getConnection(url, user, password);
   
   
        // 3、定义sql语句
        String sql = "insert into account(name,salary) values('王强',10000)";
        
        // 4、获取执行sql的对象 Statement
        Statement stmt = conn.createStatement();
        
        // 5、执行sql
        int count = stmt.executeUpdate(sql);
        
        // 6、处理结果
        // 打印受影响的行数
        System.out.println(count);
        System.out.println(count>0?"插入成功":"插入失败");
        
        // 7、释放资源
        stmt.close();
        conn.close();
        }
  }      

The control output results are as follows:
Insert image description here
Data in the table:
Insert image description here

Next article, detailed explanation of JDBC. . .

Guess you like

Origin blog.csdn.net/Tesla_Zhou/article/details/135234833