原文链接: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语句中涉及到时间时,要加上对应的格式
个人学习笔记(供个人记录使用)