java數(shù)據(jù)庫只要就是實現(xiàn)登錄功能。目前大多數(shù)網(wǎng)站都會有用戶管理功能,這些用戶的賬號和密碼需要存儲在數(shù)據(jù)庫里面,當需要連接服務(wù)器時,就需要使用數(shù)據(jù)庫連接來訪問網(wǎng)站的數(shù)據(jù)庫。那Java數(shù)據(jù)庫的存儲過程是怎樣的?下面來我們就來給大家講解一下。
ConnUtils連接工具類:用來獲取連接、釋放資源
package com.ljq.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public final class ConnUtils { private static String url = "jdbc:oracle:thin:@localhost:1521:orcl"; private static String user = "test"; private static String password = "test"; private ConnUtils() {} // 注冊驅(qū)動 (只做一次) static { try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } public static void free(ResultSet rs, Statement st, Connection conn) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
創(chuàng)建帶出參存儲過程代碼:
--帶出參存儲過程
CREATE OR REPLACe PROCEDURE stu_proc(v_name OUT VARCHAR2) AS BEGIN SELECT o.sname INTO v_name FROM student o where o.id = 2; END;
使用java調(diào)用帶出參的存儲過程
package com.ljq.test; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import java.sql.Types; public class ProceTest { public static void main(String[] args) throws Exception { Connection conn = null; CallableStatement statement = null; String sql = "{call stu_proc(?)}"; try { conn = ConnUtils.getConnection(); statement = conn.prepareCall(sql); statement.registerOutParameter(1, Types.VARCHAR); statement.executeUpdate(); //輸出:lisi String sname = statement.getString(1); System.out.println(sname); } catch (SQLException e) { e.printStackTrace(); } finally { ConnUtils.free(null, statement, conn); } } }
創(chuàng)建帶出入?yún)⒋鎯^程代碼
--帶出入?yún)⒋鎯^程
CREATE OR REPLACE PROCEDURE stu_proc(v_id IN NUMBER, v_name OUT VARCHAR2) AS BEGIN SELECT o.sname INTO v_name FROM student o where o.id = v_id; END;
使用JAVA調(diào)用帶出入?yún)⒋鎯^程
package com.ljq.test; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import java.sql.Types; public class ProceTest { public static void main(String[] args) throws Exception { Connection conn = null; CallableStatement statement = null; String sql = "{call stu_proc(?, ?)}"; try { conn = ConnUtils.getConnection(); statement = conn.prepareCall(sql); statement.setInt(1, 1); statement.registerOutParameter(2, Types.VARCHAR); statement.executeUpdate(); //輸出:zhangsan String sname = statement.getString(2); System.out.println(sname); } catch (SQLException e) { e.printStackTrace(); } finally { ConnUtils.free(null, statement, conn); } } }
這就是Java數(shù)據(jù)庫的存儲過程,另外,java數(shù)據(jù)庫是java的重要知識點,因此對于java數(shù)據(jù)庫的連接等其他操作,我們也要掌握哦!最后大家如果想要了解更多初識java知識,敬請關(guān)注賦能網(wǎng)。
本文鏈接:
本文章“Java數(shù)據(jù)庫的存儲過程是怎樣的?Java數(shù)據(jù)庫的存儲過程”已幫助 78 人
免責(zé)聲明:本信息由用戶發(fā)布,本站不承擔(dān)本信息引起的任何交易及知識產(chǎn)權(quán)侵權(quán)的法律責(zé)任!
本文由賦能網(wǎng) 整理發(fā)布。了解更多培訓(xùn)機構(gòu)》培訓(xùn)課程》學(xué)習(xí)資訊》課程優(yōu)惠》課程開班》學(xué)校地址等機構(gòu)信息,可以留下您的聯(lián)系方式,讓課程老師跟你詳細解答:
咨詢熱線:4008-569-579