jdbcを使用してデータベース関連の操作に接続します

1つは、jdbcを使用してデータベースに接続する基本的な操作です。

1.基本的な操作手順

  1. jarパッケージをインポートします
  2. ドライバーをロードします
  3. 接続オブジェクトを作成する
  4. ステートメントオブジェクトを生成する
  5. SQLを実行します
  6. プロセス結果
  7. リソースを解放する
//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();

接続オブジェクトを取得する2、3の方法

1.接続されたデータベースオブジェクトを取得する3つの方法

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();
        }
    }
}

総括する

上記は、jdbcとデータベース接続を使用する基本的な操作と、接続オブジェクトを取得するための3つの方法のすべての内容、主にデータベース接続の基本的な操作です。

おすすめ

転載: blog.csdn.net/StruggleBamboo/article/details/112392243