Java跟Oracle之间最常用的是Java调用Oracle的存储过程,以下简要说明下Java如何对Oracle存储过程进行调用。
Java跟Oracle之间最常用的是Java调用Oracle的存储过程,以下简要说明下Java如何对Oracle存储过程进行调用。
Ⅰ、Java调用Oracle存储过程【不带输出参数】
过程名称为pro1,参数个数1个,数据类型为整形数据。
import java.sql.*;
public class ProcedureNoArgs {
public static void main(String args[]) throws Exception {
// 加载Oracle驱动
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// 获得Oracle数据库连接
Connection conn = DriverManager.getConnection(
" jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd);
// 创建存储过程的对象
CallableStatement c = conn.divpareCall(" {call pro1(?)} ");
// 给Oracle存储过程的参数设置值 ,,将第一个参数的值设置成188
c.setInt(1, 188);
// 执行Oracle存储过程
c.execute();
conn.close();
}
}
Ⅱ、JAVA调用Oracle存储过程【带输出参数的情况】
过程名称为pro2,参数个数2个,数据类型为整形数据,返回值为整形类型
import java.sql.*;
public class ProcedureWithArgs {
public static void main(String args[]) throws Exception {
// 加载Oracle驱动
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// 获得Oracle数据库连接
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd);
// 创建Oracle存储过程的对象,调用存储过程
CallableStatement c = conn.divpareCall("{call pro2(?,?)}");
// 给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188
c.setInt(1, 188);
// 注册存储过程的第二个参数
c.registerOutParameter(2, java.sql.Types.INTEGER);
// 执行Oracle存储过程
c.execute();
// 得到存储过程的输出参数值并打印出来
System.out.println(c.getInt(2));
conn.close();
}
}
以上即是在JAVA中调用Oracle存储过程的最简单的实例,接下来将介绍有关JAVA跟Oracle存储过程的其他方面内容。
二、使用JAVA创建Oracle存储过程
先看下如何使用JAVA来编写Oracle存储过程,我们在Oracle的sql plus中编写语句。
Ⅰ、启动SQL PLUS
Ⅱ、编写JAVA类,定义JAVA类别名,本存储过程为简单地输出传入参数的平方值。
create or replace and compile java source named "PF" as
/**
* 通过JAVA类来 创建Oracle存储过程
*/
package org.oraclejava.pro;
public class javaCreatePro {
public static String test(int num) {
return num + "的平方为: " + num * num;
}
}
Ⅲ、将JAVA类中的方法test创建为一个Oracle函数方法
create or replace function PF_FUN(name integer) return varchar2 as language java name 'org.oraclejava.pro.javaCreatePro.test(java.lang.Integer) return java.lang.String';
Ⅳ、在控制台直接调用创建的Oracle函数
select PF_FUN(10) from dual;
Ⅴ、输出结果
10的平方为:100

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!
