溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JDBC常見面試問題有哪些

發(fā)布時間:2021-06-29 10:46:10 來源:億速云 閱讀:185 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要講解了“JDBC常見面試問題有哪些”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“JDBC常見面試問題有哪些”吧!

JDBC(Java Data Base Connectivity,java數(shù)據(jù)庫連接)是一種用于執(zhí)行SQL語句的Java API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,它由一組用Java語言編寫的類和接口組成。JDBC提供了一種基準(zhǔn),據(jù)此可以構(gòu)建更高級的工具和接口,使數(shù)據(jù)庫開發(fā)人員能夠編寫數(shù)據(jù)庫應(yīng)用程序。

初始化H2數(shù)據(jù)庫

public class BaseTest {

    protected void initH2db(Connection conn) throws SQLException, ClassNotFoundException, URISyntaxException {
        Statement st = null;
        try {
            String schema = getClass().getResource("/db/schema.sql").toURI().toString().substring(6);
            String data = getClass().getResource("/db/data.sql").toURI().toString().substring(6);

            st = conn.createStatement();

            // 這一句可以不要
            st.execute("drop all objects;");

            // 執(zhí)行初始化語句
            st.execute("runscript from '" + schema + "'");
            st.execute("runscript from '" + data + "'");
        } finally {
            if (Objects.nonNull(st)) {
                st.close();
            }
        }
    }
}

JDBC 操作數(shù)據(jù)庫流程

public class JdbcDemoTest extends BaseTest {

    @Test
    public void testJdbcDemo() {
        String sql = "select `id`, `name`, `age`, `address` from person where name = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet resultSet = null;
        try {
            // 注冊 JDBC 驅(qū)動
            Class.forName("org.h3.Driver");

            // 打開連接
            conn = DriverManager.getConnection("jdbc:h3:mem:ssb_test", "root", "root");
            initH2db(conn);

            // 創(chuàng)建 Statement
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, "wyf");

            // 執(zhí)行查詢
            resultSet = stmt.executeQuery();

            // 處理結(jié)果
            // 展開結(jié)果集數(shù)據(jù)庫
            List<Person> peoples = new ArrayList<>();
            while (resultSet.next()) {
                Person person = new Person();
                // 通過字段檢索
                person.setId(resultSet.getLong("id"));
                person.setName(resultSet.getString("name"));
                person.setAge(resultSet.getInt("age"));
                person.setAddress(resultSet.getString("address"));
                peoples.add(person);
            }
            System.out.println(JSON.toJSONString(peoples));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 關(guān)閉資源
            if (Objects.nonNull(resultSet)) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (Objects.nonNull(stmt)) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (Objects.nonNull(conn)) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

我們可以看到JDBC操作數(shù)據(jù)庫主要流程為:

  1. 注冊 JDBC 驅(qū)動(Class.forName("XXX");)

  2. 打開連接(DriverManager.getConnection("url","name","password"))

  3. 根據(jù)連接,創(chuàng)建 Statement(conn.prepareStatement(sql))

  4. 設(shè)置參數(shù)(stmt.setString(1, "wyf");)

  5. 執(zhí)行查詢(stmt.executeQuery();)

  6. 處理結(jié)果,結(jié)果集映射(resultSet.next())

  7. 關(guān)閉資源(finally)

Statement、PreparedStatement和CallableStatement的區(qū)別

  • Statement:用于執(zhí)行不帶參數(shù)的固定SQL語句,并返回它所生成的結(jié)果,每次執(zhí)行SQL時都會編譯SQL。

  • PreparedStatement:預(yù)編譯的SQL語句的對象,用于執(zhí)行帶參數(shù)的預(yù)編譯的SQL語句。高效,安全,可維護(hù)性好。

  • CallableStatement:用來調(diào)用數(shù)據(jù)庫中存儲過程的接口,如果有輸出參數(shù)要注冊,說明是輸出參數(shù)。

PreparedStatement 怎么防止的SQL注入

預(yù)編譯SQL使用?號來占位,在設(shè)置參數(shù)是通過加上單引號''來防止SQL注入。例如:

 select `id`, `name`, `age`, `address` from person where name = ?

最終執(zhí)行時會變成:

 select `id`, `name`, `age`, `address` from person where name = 'wyf or id > 0'

通過單引號 ''來有效防止了or語句的執(zhí)行。

JDBC 編程的缺點

  • 工作量大,流程長,操作數(shù)據(jù)庫存在很多重復(fù)工作

  • 業(yè)務(wù)代碼和技術(shù)代碼耦合嚴(yán)重

  • 每次需要新開連接資源,系統(tǒng)開銷大

  • 連接資源需要手動關(guān)閉,很容易忘記,帶來了隱患

因為上述原因,我們在實際項目開發(fā)過程中幾乎沒有直接使用JDBC來操作數(shù)據(jù)庫的,一般會使用數(shù)據(jù)庫連接池(druid) + ORM 框架(mybatis) 來操作數(shù)據(jù)庫。

使用連接池和ORM框架的優(yōu)點

  • 連接資源可以重復(fù)使用,減少了系統(tǒng)開銷,提升了系統(tǒng)性能

  • 再也不用擔(dān)心忘記釋放資源,極大的簡化了操作數(shù)據(jù)庫的流程

  • 使技術(shù)代碼和業(yè)務(wù)代碼解耦

源碼

https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases

spring-boot-student-mybatis工程

感謝各位的閱讀,以上就是“JDBC常見面試問題有哪些”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對JDBC常見面試問題有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI