1. 程式人生 > 遊戲資訊 >實戰技巧盤點 《街頭籃球攻略》SF技能解析

實戰技巧盤點 《街頭籃球攻略》SF技能解析

1.引用兩檔案(druid-1.0.9.jar和druid.properties)

注:這裡的mysql-connector的jar包也需要應用進去

 

2.程式碼

1)DrudiDemo

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidDemo {
    public
static void main(String args[]) throws Exception { //匯入jar包 //定義配置檔案 //載入配置檔案 Properties pro = new Properties(); InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); pro.load(is); //獲取連線池物件 DataSource ds = DruidDataSourceFactory.createDataSource(pro);
//獲取連線 Connection conn = ds.getConnection(); System.out.println(conn); } }

 

2)druid.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/JavaDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
username=root
password=root
#初始化連結數量
initialSize
=5 #最大連線數 maxActive=10 #超時時間 maxWait=3000 maxIdle=8 minIdle=3

 

3.結果:

 

 4.補充幫助類

1)JDBCUtils.java

package util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {
    //定義成員變數 DataSource
    private static DataSource ds;

    static{
        //載入配置檔案
        Properties pro = new Properties();
        try {
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //獲取 DataSource
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //獲取連線
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    //釋放資源
    public static void  close(Statement smt,Connection conn){
        close(null,smt,conn);
    }

    //釋放資源
    public static void  close(ResultSet rs, Statement smt, Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(smt != null){
            try {
                smt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //獲取連線池方法
    public static DataSource getDataSource(){
        return ds;
    }
}

2)執行

import util.JDBCUtils;

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

public class DruidDemo2 {
    public static void main(String[] args){
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //獲取連結
            conn = JDBCUtils.getConnection();
            //定義sql
            String sql = "insert into user (`Name`, `Age`) VALUES (?, ?);";
            //獲取pstmt物件
            pstmt = conn.prepareStatement(sql);
            //賦值
            pstmt.setString(1,"dzw2");
            pstmt.setInt(2,30);
            //執行
            int count = pstmt.executeUpdate();
            System.out.println(count);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt,conn);
        }
    }
}

3)結果:

 

 

 

 

參閱:https://www.bilibili.com/video/BV1vk4y117fU?p=519

https://www.bilibili.com/video/BV1vk4y117fU?p=520