連接oracle數據庫?
以下是最基本使用情況,僅供參考:
package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 定義數據類 連接數據庫 且查詢信息
* @author BR
* @date 2015-08-13
* @version 1.o
*/
public class dataBase {
public static void main(String[] args) {
Connection connection=null;
try {
//加載驅動
Class.forName("oracle.jdbc.driver.OracleDriver");
//獲取數據庫連接
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "scott");
//查詢語句
String sql="select*from dept";
//創建Statement對象
Statement statement=connection.createStatement();
ResultSet rSet=statement.executeQuery(sql);
//判斷查詢的結果集是否為空
if(rSet!=null){
while(rSet.next()){
int num=rSet.getInt("deptNo");
String name=rSet.getString("dName");
String loc=rSet.getString(3);
System.out.println(num+"\t"+name+"\t"+loc);
}
}
//關閉資源
rSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}