JDBC详解

文章目录


文章目录

  • 文章目录
  • 一、jdbc入门案例
  • 二、JDBC-API详解
    • 1.DriverManager
    • 2.Connection
    • 3.Statement
    • 4.ResultSet
    • 5.PreparedStatement
  • 三、数据库连接池
    • 1.简介
    • 2.使用


一、jdbc入门案例

JDBC详解_第1张图片
案例:

package com.study;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class TestOne {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        String url="jdbc:mysql://localhost/jdbcone";
        String userName="root";
        String passWord="root";
        Connection conn = DriverManager.getConnection(url, userName, passWord);
        //定义sql
        String sql="update users set password=666 where id=1";
        //获取执行sql的statement对象
        Statement stmt = conn.createStatement();
        //执行sql
        int count = stmt.executeUpdate(sql); //受影响的行数
        //处理结果
        System.out.println("受影响的行数:"+count);  //受影响的行数:1
        //释放资源
        stmt.close();
        conn.close();
    }
}

二、JDBC-API详解

1.DriverManager

JDBC详解_第2张图片
JDBC详解_第3张图片

2.Connection

JDBC详解_第4张图片
JDBC详解_第5张图片
案例:

package com.study;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestOne {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        String url="jdbc:mysql://localhost/jdbcone";
        String userName="root";
        String passWord="root";
        Connection conn = DriverManager.getConnection(url, userName, passWord);
        Statement stmt =null;
        try {
            //开启事务
            conn.setAutoCommit(false);
            //定义sql
            String sql1="update users set password=888 where id=1";
            String sql2="update users set password=666 where id=2";
            //获取执行sql的statement对象
            stmt = conn.createStatement();
            //执行sql
            int count1 = stmt.executeUpdate(sql1); //受影响的行数
            //处理结果
            System.out.println("受影响的行数:"+count1);  //受影响的行数:1
            int count2 = stmt.executeUpdate(sql2); //受影响的行数
            //处理结果
            System.out.println("受影响的行数:"+count2);  //受影响的行数:1
            //提交事务
            conn.commit();
        } catch (Exception e) {
            //回滚事务
            conn.rollback();
            e.printStackTrace();
        }
        //释放资源
        stmt.close();
        conn.close();
    }
}
/*
受影响的行数:1
受影响的行数:1
 */

3.Statement

JDBC详解_第6张图片

4.ResultSet

JDBC详解_第7张图片
案例1:

package com.study;

import java.sql.*;

public class ResultSetTest {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        String url="jdbc:mysql://localhost/jdbcone";
        String userName="root";
        String passWord="root";
        Connection conn = DriverManager.getConnection(url, userName, passWord);
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //定义sql
            String sql="select * from users";
            //创建statement对象
            stmt = conn.createStatement();
            //执行sql语句
            rs = stmt.executeQuery(sql);
            //遍历rs中的数据
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String password = rs.getString("password");
                String email= rs.getString("email");
                System.out.println(id);
                System.out.println(name);
                System.out.println(password);
                System.out.println(email);
                System.out.println("----------------------------------------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //释放资源
        rs.close();
        stmt.close();
        conn.close();
    }
}

案例2:

package com.study;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ResultSetTestPlus {
    public static void main(String[] args) throws Exception{
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        String url="jdbc:mysql://localhost/jdbcone";
        String userName="root";
        String passWord="root";
        Connection conn = DriverManager.getConnection(url, userName, passWord);
        Statement stmt = null;
        ResultSet rs = null;
        //创建集合对象
       List<Account> list=new ArrayList<>();
        try {
            //定义sql
            String sql="select * from users";
            //创建statement对象
            stmt = conn.createStatement();
            //执行sql语句
            rs = stmt.executeQuery(sql);
            //遍历rs中的数据
            while (rs.next()){
                Account account=new Account();
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String password = rs.getString("password");
                String email= rs.getString("email");
                //赋值
                account.setId(id);
                account.setUsername(name);
                account.setPassword(password);
                account.setEmail(email);
                //存入集合
                list.add(account);
//                [Account{id=1, username='小马哥', password='888', email='[email protected]'}, Account{id=2, username='小飞侠',
//                password='666', email='[email protected]'}, Account{id=3, username='马奎斯', password='333', email='[email protected]'}]

            }
            //打印集合
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //释放资源
        rs.close();
        stmt.close();
        conn.close();
    }
    }

5.PreparedStatement

JDBC详解_第8张图片
JDBC详解_第9张图片
案例:

package com.study;

import java.sql.*;

public class PreparedStatementLogin {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        String url="jdbc:mysql://localhost/jdbcone";
        String userName="root";
        String passWord="root";
        Connection conn = DriverManager.getConnection(url, userName, passWord);
        //接收用户输入的用户名和密码
        String name="king";
        String psw="888";
        String sql="select * from users where username=? and password=?";
        //创建pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
        //设置值
        pstmt.setString(1,name);
        pstmt.setString(2,psw);

        //执行sql语句
        ResultSet rs = pstmt.executeQuery();
        //判断是否登录成功
        if (rs.next()){
            System.out.println("登录成功!");
        }
        else {
            System.out.println("登录失败!");
        }
        rs.close();
        pstmt.close();
        conn.close();
    }
    }
//登录成功!

三、数据库连接池

1.简介

JDBC详解_第10张图片
JDBC详解_第11张图片

2.使用

JDBC详解_第12张图片
案例:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///users
username=root
password=root
# 初始化连接数
initialSize=5
# 最大连接数
maxActive=10
# 最大等地时间
maxWait=3000


package com.druidTest;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javafx.scene.layout.Priority;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidTest {
    public static void main(String[] args) throws Exception {
        //导入jar包
        //定义配置文件
        //加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("JDBCTest/src/druid.properties"));
        //获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        //获取数据库连接Connection
        Connection conn = dataSource.getConnection();
        System.out.println(conn);

    }
}

你可能感兴趣的:(JDBC,mysql,java,java-ee)