1. 程式人生 > 其它 >idea利用jdbc連線ORACLE資料庫實現一個查詢顯示

idea利用jdbc連線ORACLE資料庫實現一個查詢顯示

原文連結:https://www.cnblogs.com/yprluck/p/12634589.html

在官網上下載Oracle驅動,連結為:

https://www.oracle.com/database/technologies/jdbc-drivers-12c-downloads.html

下載後,得到一個壓縮包,解壓

我選的的是

其次在idea中匯入jar包,匯入方法為:

選擇File->Project Structure->Modules->Dependencies

然後選擇右側的+號,選擇JARS or directories,選擇自己下載jar包的路徑即可。

如下圖所示:

連線資料庫程式碼如下:

package com.ABC;

    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;

public class DBConnection {
        public static void main(String[] args) {
            ResultSet rs = null;
            Statement stmt = null;
            Connection conn = null;
            List
<String> C=new ArrayList<>(); String a="2021-10-01"; String sql = "select abc from TABLE where DATE=to_date('"+a+" 00:00:00','yyyy-MM-dd hh24:mi:ss') order by ANUM asc"; try { Class.forName("oracle.jdbc.driver.OracleDriver"); String dbURL
= "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(dbURL, "scott", "Ypr990329"); System.out.println("資料庫連線成功!"); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()){ String c=rs.getString("abc"); C.add(c); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); System.out.println("資料庫連線失敗!"); } finally { try { if (rs != null) { rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } //迴圈輸出查詢結果 for (String s:C){ System.out.println(s); } } }

筆記:

在連線ORAREL資料庫時,sql語句中資料庫的表名、列名大小寫可以不對但是下劃線必須帶上,駝峰不可以在這使用

sql語句中涉及到時間時,要加上對應的格式

個人學習筆記(供個人記錄使用)