一、鏈接方式
- Oracle jdbc配置,以下方式都可以成功連接數據庫
SID格式:
jdbc:oracle:thin:@localhost:1521:SID
如: jdbc:oracle:thin:@192.168.1.1:1521:orcl
ServiceName格式:
jdbc:oracle:thin:@//localhost:1521:SERVICENAME
如: jdbc:oracle:thin:@//192.168.1.1:1521/orcl
thin:小型驅動,驅動方式
localhost :本機ip地址 127.0.0.1
SID:數據庫的SID
SERVICENAME:數據庫的SID
- MySQL URL
jdbc:mysql://localhost:3306/數據庫名稱
技巧:
通過一種jdbc的url連接在IDEA成功連接數據庫后,點擊切換連接類型可將url轉換為其它類型的jdbc url連接;
二、驅動類型
在ORACLE中有三種類型的JDBC驅動,都使用相同的 syntax, API, and Oracle extensions,以使JAVA代碼在robust clients、Web-based Java applets, and Java stored procedures之間保持輕便靈活:
三種類型如下:
1.JDBC OCI: 此驅動類似於傳統的ODBC 驅動。因為它需要Oracle Call Interface and Net8,所以它需要在運行使用此驅動的JAVA程序的機器上安裝客戶端軟件
2.JDBC Thin: 這種驅動一般用在運行在WEB瀏覽器中的JAVA程序。它不是通過OCI or Net8,而是通過Java sockets進行通信 ,因此不需要在使用JDBC Thin的客戶端機器上安裝客戶端軟件。
3.JDBC KPRB: 這種驅動由直接存儲在數據庫中的JAVA程序使用
三、數據庫連接代碼片段
import oracle.jdbc.driver.OracleDriver;
import java.sql.*;
import java.util.Properties;
/**
* JDBC的六大步驟
* JAVA連接Oracle的三種方式
*/
public static void main(String[] args) {
Connection connect = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//第一步:注冊驅動
//第一種方式:類加載(常用)
//Class.forName("oracle.jdbc.OracleDriver");
//第二種方式:利用Driver對象
Driver driver = new OracleDriver();
DriverManager.deregisterDriver(driver);
//第二步:獲取連接
//第一種方式:利用DriverManager(常用)
//connect = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "你的oracle數據庫用戶名", "用戶名密碼");
//第二種方式:直接使用Driver
Properties pro = new Properties();
pro.put("user", "你的oracle數據庫用戶名");
pro.put("password", "用戶名密碼");
connect = driver.connect("jdbc:oracle:thin:@localhost:1521:XE", pro);
//測試connect正確與否
System.out.println(connect);
//第三步:獲取執行sql語句對象
//第一種方式:statement
//statement = connect.createStatement();
//第二種方式:PreStatement
PreparedStatement preState = connect.prepareStatement("select * from tb1_dept where id = ?");
//第四步:執行sql語句
//第一種方式:
//resultSet = statement.executeQuery("select * from tb1_dept");
//第二種方式:
//1是指sql語句中第一個?,2是指第一個?的values值
preState.setInt(1, 2);
//執行查詢語句
//resultSet = preState.executeQuery();
//執行的任何查詢語句,如果有結果集,則返回true,沒有的話返回false,注意如果是插入一條數據的話,雖然是沒有結果集,返回false,但是卻能成功的插入一條數據
boolean execute = preState.execute();
System.out.println(execute);
//第五步:處理結果集
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String city = resultSet.getString("city");
//打印輸出結果集
System.out.println( id + " " + name + " " + city );
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//第六步:最后無論如何要關閉資源
try {
if (resultSet!=null) resultSet.close();
if (statement!=null) statement.close();
if (connect!=null) connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
四、springboot使用druid連接池連接Oracle數據庫的簡單配置
#阿里連接池配置
#spring.datasource.druid.driver-class-name=oracle.jdbc.driver.OracleDriver #可配可不配,阿里的數據庫連接池會通過url自動搜尋
spring.datasource.druid.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.druid.username=scott
spring.datasource.druid.password=tiger
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=10
spring.datasource.druid.max-wait=10
#是否緩存preparedStatement,也就是PSCache。PSCache對支持游標的數據庫性能提升巨大,比如說oracle。
#在mysql5.5以下的版本中沒有PSCache功能,建議關閉掉。
#spring.datasource.druid.pool-prepared-statements=true
#配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
#spring.datasource.druid.time-between-eviction-runs-millis=60000
#配置一個連接在池中最小生存的時間,單位是毫秒
#spring.datasource.druid.min-evictable-idle-time-millis=300000
#配置擴展插件:監控統計用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall
#spring.datasource.druid.filters=stat,wall
#spring.datasource.druid.filter.stat.log-slow-sql=true
#spring.datasource.druid.filter.stat.slow-sql-millis=2000
#自定義配置端口號
server.port=8889
#配置項目訪問時上下文路徑(項目名稱)
#server.servlet.context-path=/sbm
#mybatis相關的配置
#實體別名配置 配置上后,以后在mapper文件中的resultType中原來配置的com.guangming.springboot.entity.Dept就可以直接寫成dept
mybatis.type-aliases-package=com.guangming.springboot.entity
#mybatis掃描mapper文件的配置
mybatis.mapper-locations=classpath:mapper/*.xml
#靜態資源目錄配置
spring.resources.static-locations=classpath:/templates/,classpath:/static/,classpath:/resources/