java中oracle.链接,java连接Oracle数据库的方法解析
本文主要對java連接Oracle數(shù)據(jù)庫方法進(jìn)行步驟解析,具有很好的參考價值,需要的朋友一起來看下吧
Oracle數(shù)據(jù)庫先創(chuàng)建一個表和添加一些數(shù)據(jù)
1.先在Oracle數(shù)據(jù)庫中創(chuàng)建一個student表:
create table student
(
id number(11) not null primary key,
stu_name varchar(16) not null,
gender number(11) default null,
age number(11) default null,
address varchar(128) default null
);
2.向表中增添一些數(shù)據(jù)
insert into student values('1','王小軍','1','17','北京市和平里七區(qū)30號樓7門102')
MyEclipse里編寫java代碼
1.將ojdbc6.jar導(dǎo)入項目中
先創(chuàng)建一個項目,然后在鼠標(biāo)移到項目上右鍵-->new-->folder;folder name:lib;這樣就在項目中創(chuàng)建了一個文件夾lib;然后將ojdbc6.jar包導(dǎo)入該文件夾中
鼠標(biāo)移到該包上;右鍵-->build path-->add to build path;
2.創(chuàng)建一個類,開始編碼
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class OperateOracle {
// 定義連接所需的字符串
// 192.168.0.X是本機地址(要改成自己的IP地址),1521端口號,XE是精簡版Oracle的默認(rèn)數(shù)據(jù)庫名
private static String USERNAMR = "orcl";
private static String PASSWORD = "orcl";
private static String DRVIER = "oracle.jdbc.OracleDriver";
private static String URL = "jdbc:oracle:thin:@192.168.0.X:1521:xe";
// 創(chuàng)建一個數(shù)據(jù)庫連接
Connection connection = null;
// 創(chuàng)建預(yù)編譯語句對象,一般都是用這個而不用Statement
PreparedStatement pstm = null;
// 創(chuàng)建一個結(jié)果集對象
ResultSet rs = null;
/**
* 向數(shù)據(jù)庫中增加數(shù)據(jù)
* 首先獲取表內(nèi)數(shù)據(jù)總數(shù),總數(shù)+1為新增數(shù)據(jù)的id值
* @param stuName:學(xué)生姓名
* @param gender:學(xué)生性別,1表示男性,2表示女性
* @param age:學(xué)生年齡
* @param address:學(xué)生住址
*/
public void AddData(String stuName, int gender, int age, String address) {
connection = getConnection();
// String sql =
// "insert into student values('1','王小軍','1','17','北京市和平里七區(qū)30號樓7門102')";
String sql = "select count(*) from student where 1 = 1";
String sqlStr = "insert into student values(?,?,?,?,?)";
int count = 0;
try {
// 計算數(shù)據(jù)庫student表中數(shù)據(jù)總數(shù)
pstm = connection.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
count = rs.getInt(1) + 1;
System.out.println(rs.getInt(1));
}
// 執(zhí)行插入數(shù)據(jù)操作
pstm = connection.prepareStatement(sqlStr);
pstm.setInt(1, count);
pstm.setString(2, stuName);
pstm.setInt(3, gender);
pstm.setInt(4, age);
pstm.setString(5, address);
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ReleaseResource();
}
}
/**
* 向數(shù)據(jù)庫中刪除數(shù)據(jù)
* @param stuName:根據(jù)姓名刪除數(shù)據(jù)
*/
public void DeleteData(String stuName) {
connection = getConnection();
String sqlStr = "delete from student where stu_name=?";
System.out.println(stuName);
try {
// 執(zhí)行刪除數(shù)據(jù)操作
pstm = connection.prepareStatement(sqlStr);
pstm.setString(1, stuName);
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ReleaseResource();
}
}
/**
* 向數(shù)據(jù)庫中修改數(shù)據(jù)
* @param stuName:學(xué)生姓名,根據(jù)此值查詢要修改的某行值
* @param gender
* @param age
* @param address
*/
public void UpdateData(String stuName, int gender, int age, String address) {
connection = getConnection();
String sql = "select id from student where 1 = 1 and stu_name = ?";
String sqlStr = "update student set stu_name=?,gender=?,age=?,address=? where id=?";
int count = 0;
try {
// 計算數(shù)據(jù)庫student表中數(shù)據(jù)總數(shù)
pstm = connection.prepareStatement(sql);
pstm.setString(1, stuName);
rs = pstm.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
System.out.println(rs.getInt(1));
}
// 執(zhí)行插入數(shù)據(jù)操作
pstm = connection.prepareStatement(sqlStr);
pstm.setString(1, stuName);
pstm.setInt(2, gender);
pstm.setInt(3, age);
pstm.setString(4, address);
pstm.setInt(5, count);
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ReleaseResource();
}
}
/**
* 向數(shù)據(jù)庫中查詢數(shù)據(jù)
*/
public void SelectData() {
connection = getConnection();
String sql = "select * from student where 1 = 1";
try {
pstm = connection.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("stu_name");
String gender = rs.getString("gender");
String age = rs.getString("age");
String address = rs.getString("address");
System.out.println(id + "\t" + name + "\t" + gender + "\t"
+ age + "\t" + address);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ReleaseResource();
}
}
/**
* 使用ResultSetMetaData計算列數(shù)
*/
public void SelectData2() {
connection = getConnection();
String sql = "select * from employees where 1 = 1";
int count = 0;
try {
pstm = connection.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
count++;
}
ResultSetMetaData rsmd = rs.getMetaData();
int cols_len = rsmd.getColumnCount();
System.out.println("count=" + count + "\tcols_len=" + cols_len);
} catch (SQLException e) {
e.printStackTrace();
} finally {
ReleaseResource();
}
}
/**
* 獲取Connection對象
*
* @return
*/
public Connection getConnection() {
try {
Class.forName(DRVIER);
connection = DriverManager.getConnection(URL, USERNAMR, PASSWORD);
System.out.println("成功連接數(shù)據(jù)庫");
} catch (ClassNotFoundException e) {
throw new RuntimeException("class not find !", e);
} catch (SQLException e) {
throw new RuntimeException("get connection error!", e);
}
return connection;
}
/**
* 釋放資源
*/
public void ReleaseResource() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstm != null) {
try {
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.創(chuàng)建一個測試類
public class Test {
public static void main(String[] args) {
/**
* 增刪改查完成,但是有一定局限性
* 1.增 問題不大
* 2.刪 要給出一個值去刪除(可能值不存在-->沒有處理機制,值不唯一怎么處理?)
* 3.改 同刪的問題
* 4.查 問題不大
*/
//創(chuàng)建OperateOracle對象
OperateOracle oo=new OperateOracle();
//測試增加數(shù)據(jù)操作
//oo.AddData("孫中山",1,25,"北京市海淀區(qū)紅旗路111號");
//測試刪除數(shù)據(jù)操作
//oo.DeleteData("孫中山");
//測試更新數(shù)據(jù)操作
oo.UpdateData("孫中山",1,30,"北京市東城區(qū)岳山路11號");
//測試查詢數(shù)據(jù)操作
//oo.SelectData();
//測試ResultSetMetaData類
//oo.SelectData2();
}
}
正如測試類中所注釋的,此處只可按照正確的方式去連接Oracle數(shù)據(jù)庫,操作增刪改查操作,但是對于一些錯誤操作的處理機制還不夠完善。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
總結(jié)
以上是生活随笔為你收集整理的java中oracle.链接,java连接Oracle数据库的方法解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 6.0新特性,新版特性 | 序言
- 下一篇: java怎么播放不了声音,怎么在java