Create Java Source
?
class="java">create or replace and compile java source named firstjava as import oracle.sql.*; import java.io.*; import oracle.jdbc.driver.*; import java.sql.*; public class FirstJava { /* * 测试方法1,没有数据库交互 */ public static String hello(String yourName) { return "Hello world "+yourName; } /* * 测试方法2,同数据库有交互, * 注意该方法中获得OracleDriver 和 Connection的方法同标准Java的区别 * 标准JDBC中的调用方法应该如下: * //获得数据库连接 * java.lang.Class.forName(\"oracle.jdbc.driver.OracleDriver\"); * con = DriverManager.getConnection(\"jdbc:oracle:thin:scott/tiger@localhost:1521:LEGDB\"); */ public static String get_current_time() { String current_time=""; OracleDriver driver = new OracleDriver(); Connection connection=null; Statement stmt=null; try { connection = DriverManager.getConnection("jdbc:default:connection:"); //connection = driver.defaultConnection(); stmt = connection.createStatement(); String cmd = "select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual"; ResultSet rs=stmt.executeQuery(cmd); if(rs.next()){ current_time=rs.getString(1); } rs.close(); } catch (SQLException e) { System.out.println(" There is an error " + e); }finally { try{ if (stmt != null) stmt.close(); if (connection != null) connection.close(); }catch(Exception ee){} } return current_time; } }
?
?
Create function
create or replace function MY_HELLO(v_name in varchar2) return VARCHAR2 as language java name 'FirstJava.hello(java.lang.String) return java.lang.String';
?
?