我在Oracle数据库中创建了两种类型的OBJECT和一种表格和一个嵌套表格。我知道如何在cmd中插入表中的行,但我不知道如何在Java中使用PreparedStatement
在此表中插入行。
这是我的表格和类型:
CREATE Or REPLACE TYPE item AS OBJECT
(
id number,
counter number
);/
CREATE Or REPLACE TYPE time AS OBJECT
(
year number,
month number,
second number
);
CREATE Or REPLACE TYPE Shoping_list AS TABLE OF item;
create table invoice(
id number NOT NULL PRIMARY KEY ,
list shoping_list,
dateIn date,
timeIn time,
totalCost number )
nested table list store as list_of_product;
我可以在cmd中插入此代码
insert into invoice(id,list,dateIn,timeIn,totalCost) values(seq_ID_invoice.nextval,
shoping_list(item(1,2),item(26,1)),TO_DATE('1396-04-11','YYYY-MM-DD'),time(11,25,40),7000);
这是我的java代码
preparedStatement = connection.prepareStatement("insert into invoice (id,list,dateIn,timeIn,totalCost) values ("+id+",?,?,?,?)");
答案 0 :(得分:0)
seq_ID_invoice
是Oracle序列。因此oracle会自动获取其值。如需休息,您可以根据表格中定义的列类型,使用setInt, setString, setFloat, etc. methods
中提供的PreparedStatement
。
以下示例代码可以帮助您。
PreparedStatement ps = connection.prepareStatement(
"insert into invoice(id, list, dateIn, timeIn, totalCost)"+
" values (seq_ID_invoice.nextval, shoping_list(item(?,?), item(?,?)), TO_DATE(?,'YYYY-MM-DD'), time(?, ?, ?), ?)");
ps.setInt(1, int_value_for_id_of_item_1);
ps.setInt(2, int_value_for_counter_of_item_1);
ps.setInt(3, int_value_for_id_of_item_2);
ps.setInt(4, int_value_for_counter_of_item_2);
ps.setString(5, string_value_for_date);
ps.setInt(6, int_value_for_year_of_time);
ps.setInt(7, int_value_for_month_of_time);
ps.setInt(8, int_value_for_second_of_time);
ps.setInt(9, int_bvalue_for_total_cost);
int rowCnt = ps.executeUpdate();
System.out.println(rowCnt+" rows affected.");
int_value_for_id_of_item_1, int_value_for_counter_of_item_1, ...
这些是java变量。您可以定义您选择的变量,也可以直接提及值来代替这些变量。