简体   繁体   English

无法在postgresql数据库中插入值

[英]not able to insert values in my postgresql database

In java code: 在java代码中:

CallableStatement cs=null;
int bcode=1;
int dcode=3;
int noftab=3;
String sql2 = "{? = call public.insertdepttables('"+bcode+"','"+dcode+"','"+noftab+"')}";
cs = conn.prepareCall(sql2);
cs.registerOutParameter(1,Types.INTEGER);
rset1=cs.getInt(1);
system.out.println("success="+rest1);

In insertdepttables:(Function in my db-postgresql) 在insertdepttables中:(我的db-postgresql中的函数)

CREATE OR REPLACE FUNCTION public.insertdepttables
(
  IN  branch_code  integer,
  IN  dept_code    integer,
  IN  no_tables    integer
)
RETURNS integer AS
$$
DECLARE
count integer;
begin
     IF no_tables>0 THEN     
     LOOP
     insert into t_dept_tables(branch_code,dept_code,table_no)values(branch_code,dept_code,no_tables);

     no_tables=no_tables-1; 
     Count=count+1;
     EXIT when no_tables =0;
     END LOOP ;
     END IF;
RETURN count;
end
$$

actually i need to insert the no_table (ie,3) times row in my t_dept_tables in postgresql. 实际上我需要在postgresql中的t_dept_tables中插入no_table(即3次)行。 So wat i have done is i hav written functions in database. 我所做的就是我在数据库中编写了函数。 I need to call it from java code. 我需要从java代码中调用它。 So I used callable stmt there: 所以我在那里使用了callable stmt:

My output should be like this in db: 我的输出在db中应该是这样的:

bcode dcode table_no
1      3     1
1      3     2
1      3     3

Now while executing java code, I'm getting the result as, 现在在执行java代码时,我得到的结果是,

console: 安慰:

success=3

but 3 rows arent inserted in db, but while executing the function in my db, the values get inserted. 但是在db中没有插入3行,但在我的db中执行该函数时,会插入值。 So no wrong in my db function. 所以我的db函数没有错。 please help me to sort this out. 请帮我解决这个问题。

Are you sure you haven't forgot to actually call statement, like: 你确定你没有忘记实际调用语句,如:

cs.executeQuery();
...
cs.close();

BTW, your task could be achieved without functions, like (don't use string concatenation, use parameter values): 顺便说一下,你的任务可以在没有函数的情况下实现,比如(不要使用字符串连接,使用参数值):

String stm = "insert into t_dept_tables(branch_code,dept_code,table_no) values(branch_code,dept_code,no_tables); VALUES(?, ?, ?)";
cs = con.prepareStatement(stm);
cs.setInt(1, id);
cs.setInt(3, id);
cs.setInt(3, id);
rs = cs.executeUpdate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM