欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

jdbc使用模板-創(chuàng)新互聯(lián)

JDBC模板

使用方法:

公司主營業(yè)務:成都網(wǎng)站設計、成都網(wǎng)站建設、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出橋東免費做網(wǎng)站回饋大家。

1.在項目的resources文件里添加data.properties文件,內(nèi)容為

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫名?characterEncoding=utf-8
user=用戶名
password=密碼

2.項目采取單例模式,getStatement方法獲取查詢sql操作對象
3.getPreparedStatement方法獲取增刪改操作對象,預操作防止sql注入
4.使用完之后建議調(diào)用closeConnection方法關閉鏈接。

//
// Created by bianys on 2022/12/20.
// author: bianys
//

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {private static Connection conn = null;
    private static Statement stmt = null;
    private static ResultSet rs = null;
    private static PreparedStatement ps = null;
    private static Properties p = new Properties();
    private static FileInputStream fis= null;
    //單例模式
    static {try {Path path = Paths.get("src/main/resources/data.properties");
            fis = new FileInputStream(path.toAbsolutePath().toString());
        } catch (FileNotFoundException e) {e.printStackTrace();
        }
        try {p.load(fis);
        } catch (IOException e) {e.printStackTrace();
        }
        String driver=p.getProperty("driver");
        String user=p.getProperty("user");
        String url=p.getProperty("url");
        String password=p.getProperty("password");
        //1.注冊驅動
        //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        try {Class.forName(driver);
            //2.獲取連接
            conn= DriverManager.getConnection(url,user,password);
        } catch (ClassNotFoundException | SQLException e) {e.printStackTrace();
        }

    }
    public static Statement getStatement(){try
        {//3.創(chuàng)建數(shù)據(jù)庫操作對象
            return conn.createStatement();
        }catch (SQLException e){e.printStackTrace();
        }
        return null;
    }

    public static PreparedStatement getPreparedStatement(String sql){try
        {//3.創(chuàng)建數(shù)據(jù)庫操作對象
            return conn.prepareStatement(sql);
        }catch (SQLException e){e.printStackTrace();
        }
        return null;
    }

    public static void closeConnection(){if (stmt!=null)
        {try
            {stmt.close();
            }
            catch (SQLException e)
            {e.printStackTrace();
            }
        }
        if (ps!=null)
        {try
            {ps.close();
            }
            catch (SQLException e)
            {e.printStackTrace();
            }
        }
        if (conn!=null)
        {try
            {conn.close();
            }
            catch (SQLException e)
            {e.printStackTrace();
            }

        }
        if (rs!=null)
        {try
            {rs.close();
            }
            catch (SQLException e)
            {e.printStackTrace();
            }
        }
    }
}

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

當前標題:jdbc使用模板-創(chuàng)新互聯(lián)
當前鏈接:http://www.chinadenli.net/article46/digheg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT定制開發(fā)品牌網(wǎng)站設計品牌網(wǎng)站制作自適應網(wǎng)站關鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設