跳轉到內容

Java JDBC 使用 SQLite/示例基類

來自華夏公益教科書,開放的書籍,為開放的世界

此頁面提供了一個示例基類,您可以將其用作建立自己的基類的骨架,以及擴充套件此類的思路。您應該注意,您主要的關注點應該是正確地設定初始上下文,並且有許多不同的方法來解決這個問題。此基類是抽象的,因為我們希望將其擴充套件以更精確地滿足我們的需求。但是,其中沒有任何特別深奧的東西來阻止您透過宣告它為 public 並直接使用它來具體化此類,並相應地修改和提供您自己的建構函式。

您還應該注意,此基類中有一些宣告為 public 的欄位和方法,這些欄位和方法更適合宣告為 private,但是這些欄位和方法被保留為 public 用於可見性,因為您可能希望嘗試一些呼叫的含義。此外,將這些欄位和方法保留為 public 使您能夠透過反射來呼叫和調整它們。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/** Database connection class & utilities **/

abstract class Db {

    public  String sDriver = ""; 
    public  String sUrl = null;
    public  int iTimeout = 30;
    public  Connection conn = null;
    public  Statement statement = null;


    /* Stub constructor for quick instantiation o/t fly for using some of the ancillary stuff */

    public  Db()
    {}

    /* quick and dirty constructor to test the database passing the DriverManager name and the fully loaded url to handle */
    /* NB this will typically be available if you make this class concrete and not abstract */
    public Db(String sDriverToLoad, String sUrlToLoad) throws Exception
    {
        init(sDriverToLoad, sUrlToLoad);
    }
    
    public void init(String sDriverVar, String sUrlVar) throws Exception
    {
        setDriver(sDriverVar);
        setUrl(sUrlVar);
        setConnection();
        setStatement();
    }

    private void setDriver(String sDriverVar)
    {
        sDriver = sDriverVar;
    }

    private void setUrl(String sUrlVar)
    {
        sUrl = sUrlVar;
    }

    public  void setConnection() throws Exception {
        Class.forName(sDriver);
        conn = DriverManager.getConnection(sUrl);
    }


    public  Connection getConnection() {
        return conn;
    }

    public  void setStatement() throws Exception {
        if (conn == null) {
            setConnection();
        }
        statement = conn.createStatement();
        statement.setQueryTimeout(iTimeout);  // set timeout to 30 sec.
    }

    public  Statement getStatement() {
        return statement;
    }

    public  void executeStmt(String instruction) throws SQLException {
        statement.executeUpdate(instruction);
    }

    // processes an array of instructions e.g. a set of SQL command strings passed from a file
    //NB you should ensure you either handle empty lines in files by either removing them or parsing them out 
    // since they will generate spurious SQLExceptions when they are encountered during the iteration....
    public void executeStmt(String[] instructionSet) throws SQLException {
        for (int i = 0; i < instructionSet.length; i++) {
            executeStmt(instructionSet[i]);
        }
    }

    public ResultSet executeQry(String instruction) throws SQLException {
        return statement.executeQuery(instruction);
    } 

    public void closeConnection() {
        try { conn.close(); } catch (Exception ignore) {}
    }

}
華夏公益教科書