1.jdbc summary (template, eight-legged essay):
try{ a. Import the driver package and load the specific driver class Class.forName("specific driver class"); b. Establish a connection with the database connection = DriverManager.getConnection(...); c. Get the object of operating the database through the connection (Statement\preparedStatement\callablestatement) stmt = connection.createStatement(); d.(Query) processing result set rs = pstmt.executeQuery() while(rs.next()){ rs.getXxx(..) ;} }catch (ClassNotFoundException e) {...} catch(SQLException e) {... } catch(Exception e) {... } finally { //The opening sequence is opposite to the closing sequence if(rs!=null)rs.close () if(stmt!=null) stmt.close(); if(connection!=null)connection.close(); }
- In jdbc, except Class.forName() throws ClassNotFoundException, all other methods throw SQLException
2.CallableStatement: call stored procedures, stored functions
connection.prepareCall(参数:存储过程或存储函数名)
Parameter format:
stored procedure (return without return value, replace with out parameter):
{call stored procedure name (parameter list)}
stored function (with return value return):
{? = Call stored function name (parameter list)}
Stored procedure:
create or replace procedure addTwoNum ( num1 in number,num2 in number,result out number ) -- 1 + 2 ->3
as
begin
result := num1+num2 ;
end ;
Emphasize:
If you access the database through sqlplus, you only need to open: OracleServiceSID
to access data through other programs (sqldevelop, navicate, JDBC), you need to open: OracleServiceSID, XxxListener
Steps for JDBC to call a stored procedure:
a. Generate the object that calls the stored procedure (CallableStatement) cstmt = connection.prepareCall( "..." );
b. Process the output parameter value through setXxx() cstmt.setInt(1, 30);
c. Through registerOutParameter(.. .) Process output parameter type
d.cstmt.execute() execute
e. Accept output value (return value) getXxx()
JDBC call storage function:
create or replace function addTwoNumfunction (num1 in number,num2 in number) - 1 + 2
return number
as
result number;
begin
result := num1+num2;
return result;
end;
/
JDBC calling a stored function: the difference between calling a stored procedure :
When calling, pay attention to the parameters: "{? = call addTwoNumfunction (?,?) }"
3. Handling CLOB/BLOB types
Processing slightly larger data:
a. Storage path E:\JDK_API_zh_CN.CHM
stores the file path through JDBC, and then processes it according to IO operations.
For example: JDBC stores the E:\JDK_API_zh_CN.CHM file as a string "E:\JDK_API_zh_CN.CHM"
Obtain from the database : 1. Obtain the path "E:\JDK_API_zh_CN.CHM" 2.IO
b.
CLOB: large text data (novel -> data)
BLOB: binary
clob: large text data character stream Reader Writer
storage
1. First pass the pstmt? to replace the novel content (placeholder)
2. Then pass pstmt.setCharacterStream(2, reader, (int)file.length()); to the previous step of? Replace with a novel stream, note that the third parameter needs to be of type Int
Take:
1. Pass Reader reader = rs.getCharacterStream("NOVEL"); Save the data of type cloc into the Reader object
2. Just output the Reader through Writer.
blob: Binary byte stream InputStream OutputStream
and CLOB steps are basically the same, the difference: setBinaryStream(...) getBinaryStream(...)