成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

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

JDBC模板

使用方法:

公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(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方法獲取增刪改操作對象,預(yù)操作防止sql注入
4.使用完之后建議調(diào)用closeConnection方法關(guān)閉鏈接。

//
// 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.注冊驅(qū)動
        //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)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

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

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

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)
大城县| 偃师市| 汉中市| 晴隆县| 东明县| 兴安县| 岳池县| 理塘县| 红安县| 莲花县| 柞水县| 怀来县| 张掖市| 大庆市| 额敏县| 郴州市| 东乌珠穆沁旗| 芮城县| 阿拉尔市| 扶绥县| 闵行区| 丁青县| 手游| 平邑县| 镇巴县| 九台市| 安仁县| 台东县| 阿鲁科尔沁旗| 万源市| 同德县| 安新县| 华安县| 修水县| 枣庄市| 武鸣县| 邵东县| 稻城县| 宜都市| 舞阳县| 阿拉善左旗|