Use jdbc to connect to the database related operations

One, the basic operation of using jdbc to connect to the database

1. Basic operation steps

  1. Import the jar package
  2. Load the driver
  3. Build connection objects
  4. Generate statement object
  5. Execute sql
  6. process result
  7. Release resources
//1.导入jar包
//2.注册驱动
Class.forName("com.mysql.jdbc.Driver");

//3.创建连接
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");
System.out.println(con);

//4.定义sql
String sql="update account set balance=balance+500 where id=1";

//5.获取执行sql的对象 stmt
Statement stmt = con.createStatement();

//6.执行sql
int count = stmt.executeUpdate(sql);

//7.结果处理
System.out.println(count);

//8.释放资源
stmt.close();
con.close();

Two, three ways to get the connection object

1. Three ways to get connected database objects

package com;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class Hello {
    
    
    @Test//获取连接对象方式1
    public void test1() {
    
    
        try {
    
    
            //1.注册驱动 Driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.DriverManager获取连接数据库对象
            String url="jdbc:mysql://localhost:3306/db3?useSSL=false&serverTimezone=UTC";
            Connection con = DriverManager.getConnection(url, "root", "123456");
            System.out.println(con);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

    @Test//获取连接对象方式2
    public void test2(){
    
    
        try {
    
    
            //1.注册驱动 Driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.DriverManager获取连接数据库对象 localhost:3306可以省略
            String url="jdbc:mysql://localhost:3306/db3?useSSL=false&serverTimezone=UTC";
            //3.创建Properties对象,关联用户名和密码
            Properties p = new Properties();
            p.setProperty("user","root");
            p.setProperty("password","123456");
            Connection con = DriverManager.getConnection(url, p);
            System.out.println(con);

        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

    @Test//获取连接对象方式3
    public void test3(){
    
    
        try {
    
    
            //1.注册驱动 Driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.DriverManager获取连接数据库对象
            String url="jdbc:mysql://localhost:3306/db3?useSSL=false&serverTimezone=UTC";
            //3.使用类加载器加载jdbc.properties文件,返回一个字节流,和Properties关联在一起
            //InputStream is = Hello.class.getClassLoader().getResourceAsStream("jdbc.properties");
            InputStream is = new FileInputStream("src/jdbc.properties");
            System.out.println(is);
            Properties p = new Properties();
            p.load(is);
            System.out.println(p);

            Connection con = DriverManager.getConnection(url,p);
            System.out.println(con);

        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

to sum up

The above is the basic operation of using jdbc and database connection and all the contents of the three ways to obtain the connection object, mainly the basic operation of database connection.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/112392243