java連接Oracle數(shù)據(jù)庫的方法解析
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)建一個項目,然后在鼠標移到項目上右鍵-->new-->folder;folder name:lib;這樣就在項目中創(chuàng)建了一個文件夾lib;然后將ojdbc6.jar包導(dǎo)入該文件夾中
該包下載地址鏈接:http://wd.jb51.net:81//201612/yuanma/ojdbc6_jb51.rar
鼠標移到該包上;右鍵-->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的默認數(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(); } } } }
正如測試類中所注釋的,此處只可按照正確的方式去連接Oracle數(shù)據(jù)庫,操作增刪改查操作,但是對于一些錯誤操作的處理機制還不夠完善。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
SpringSecurity中的Filter Chain(過濾器鏈)
Spring Security的Filter Chain是由一系列過濾器組成的管道,每個過濾器執(zhí)行特定的安全功能,Spring Security能夠提供強大而靈活的安全控制機制,從而保護你的應(yīng)用程序不受各種網(wǎng)絡(luò)安全威脅的侵害,本文介紹SpringSecurity中的Filter Chain,感興趣的朋友跟隨小編一起看看吧2024-06-06使用maven開發(fā)springboot項目時pom.xml常用配置(推薦)
這篇文章主要介紹了使用maven開發(fā)springboot項目時的pom.xml常用配置,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01通過xml配置SpringMVC注解DispatcherServlet初始化過程解析
這篇文章主要為大家介紹了通過xml配置SpringMVC注解DispatcherServlet初始化過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10