java 数组传入oracle存储过程 所用库,java调用oracle存储过程 传递 自定义数组,对象...

一, 引入jar包  nls_charset12.jar 、orai18n.jar

二、oracle 创建对象

create or replace type questionAnswer as object(//自定义对象 题目答案

answer_no  varchar2(10),

answer_content varchar2(1000)

)

create or replace type questionAnswer_array  as array(10) of questionanswer //定义数组存放题目答案

create or replace type SingleQuestion  as object  //题目对象

(

question_type varchar2(1),

question_name varchar2(200),

right_key  varchar2(10),

questionAnswer_list  questionAnswer_array

)

create or replace type SINGLEQUESTION_ARRAY  as array(100) of SINGLEQUESTION //存放题目数组

三、Java代码

//题目实体类

import java.util.List;

public class SingleQuestion {

private String question_type;//题目类型

private String question_name;//题目名称

private String right_key;//正确答案

private ListquestionAnswer;//答案内容

public String getQuestion_type() {

return question_type;

}

public void setQuestion_type(String question_type) {

this.question_type = question_type;

}

public String getQuestion_name() {

return question_name;

}

public void setQuestion_name(String question_name) {

this.question_name = question_name;

}

public String getRight_key() {

return right_key;

}

public void setRight_key(String right_key) {

this.right_key = right_key;

}

public ListgetQuestionAnswer() {

return questionAnswer;

}

public void setQuestionAnswer(ListquestionAnswer) {

this.questionAnswer = questionAnswer;

}

public SingleQuestion() {

super();

}

}

//题目答案实体类

public class QuestionAnswer {

private String answer_no;//答案序号

private String answer_content;//答案内容

public String getAnswer_no() {

return answer_no;

}

public void setAnswer_no(String answer_no) {

this.answer_no = answer_no;

}

public String getAnswer_content() {

return answer_content;

}

public void setAnswer_content(String answer_content) {

this.answer_content = answer_content;

}

public QuestionAnswer() {

super();

}

public QuestionAnswer(String answer_no, String answer_content) {

super();

this.answer_no = answer_no;

this.answer_content = answer_content;

}

@Override

public String toString() {

return "QuestionAnswer [answer_no=" + answer_no + ", answer_content=" + answer_content + "]";

}

}

//调用oracle工具类

public class ObjectConvert {

static final String driver_class  = "oracle.jdbc.driver.OracleDriver";

static final String connectionURL = "jdbc:oracle:thin:@localhost:1521:orcl";

static final String userID        = "user";

static final String userPassword  = "password";

public static void runTest(String subject_no,Listsingle) {

Connection  con = null;

CallableStatement stmt = null ;

try {

Class.forName(driver_class);

con = (Connection) DriverManager.getConnection(connectionURL, userID, userPassword);

StructDescriptor tDescriptor1 = StructDescriptor.createDescriptor("SINGLEQUESTION", con);

Liststructs = new ArrayList();

Object[] tObject  = null ;

for(int i = 0; itObject = new Object[4];

tObject[0] = single.get(i).getQuestion_type();

tObject[1] = single.get(i).getQuestion_name();

tObject[2] = single.get(i).getRight_key();

StructDescriptor tDescripto = StructDescriptor.createDescriptor("QUESTIONANSWER", con);

Liststruc = new ArrayList();

Object[] o=null;

for(int j=0;jo=new Object[2];

o[0]=single.get(i).getQuestionAnswer().get(j).getAnswer_no();

o[1]=single.get(i).getQuestionAnswer().get(j).getAnswer_content();

STRUCT tStruct1 = new STRUCT(tDescripto, con, o);

struc.add(tStruct1);

}

ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("QUESTIONANSWER_ARRAY", con);

ARRAY tArray = new ARRAY(arrayDescriptor, con, struc.toArray());

tObject[3]=tArray;

STRUCT tStruct = new STRUCT(tDescriptor1, con, tObject);

structs.add(tStruct);

}

ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("SINGLEQUESTION_ARRAY", con);

ARRAY tArray = new ARRAY(arrayDescriptor, con, structs.toArray());

stmt = con.prepareCall("{call p_import_question(?,?)}");

stmt.setString(1, subject_no);

stmt.setArray(2, tArray);

stmt.execute();

}  catch (SQLException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}finally{

if(stmt != null){

try {

stmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(con != null){

try {

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

参考文章:http://blog.csdn.net/stevendbaguo/article/details/46968219

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值