在PreparedStatement中使用PLSQL块

时间:2016-04-13 08:54:08

标签: java

我正在尝试在Java中的PreparedStatement中使用PL / SQL块。架构名称将来自外部。这是代码:

PreparedStatement statement = null;
try {
    statement = connection.prepareStatement("
                        declare 
                        p_schema_name varchar2(400):= upper('" + schema + "'); 
                        l_owner varchar2(400);
                        l_object_name varchar2(400);
                        cursor c1_views is
                            select
                                owner,
                                object_name
                            from dba_objects
                            where object_type in (''VIEW'')
                            and status = ''INVALID''
                            and owner = p_schema_name;
                    begin
                        open c1_views;
                        loop
                            fetch c1_views into l_owner, l_object_name;  
                            if c1_views%notfound then
                                exit;
                            else
                                execute immediate 'alter view '||l_owner||'.'||l_object_name||' compile ';
                            end if;
                        end loop;
                        begin
                            dbms_utility.compile_schema(schema => p_schema_name);
                        end;
                    end;");
                statement.execute();
            } catch (SQLException e) {
                System.out.println("ERROR: Unable to compile schema " + schema + " in afterMigrate: " + e.getMessage());
            } finally {
                if (null != statement) {
                    try {
                        statement.close();
                    } catch (SQLException se) {
                        System.out.println("ERROR: Unable to close statement in afterMigrate: " + se.getMessage());
                    }
                }
            }

这些是我得到的错误

javac example.java
example.java:25: error: unclosed string literal
                                statement = connection.prepareStatement("
                                                                        ^
example.java:25: error: ';' expected
                                statement = connection.prepareStatement("
                                                                         ^
example.java:27: error: ';' expected
                                                p_schema_name varchar2(400):= up
per('" + schema + "');
                                                                      ^
example.java:27: error: unclosed character literal
                                                p_schema_name varchar2(400):= up
per('" + schema + "');

    ^
example.java:27: error: unclosed string literal
                                                p_schema_name varchar2(400):= up
per('" + schema + "');

                  ^
example.java:27: error: not a statement
                                                p_schema_name varchar2(400):= up
per('" + schema + "');

                ^
example.java:28: error: ';' expected
                                                l_owner varchar2(400);
                                                                ^
example.java:29: error: ';' expected
                                                l_object_name varchar2(400);
                                                                      ^
example.java:30: error: ';' expected
                                                cursor c1_views is
                                                               ^
example.java:31: error: ';' expected
                                                        select
                                                              ^
example.java:32: error: not a statement
                                                                owner,
                                                                ^
example.java:32: error: ';' expected
                                                                owner,
                                                                     ^
example.java:34: error: ';' expected
                                                        from dba_objects
                                                            ^
example.java:35: error: ';' expected
                                                        where object_type in (''
VIEW'')
                                                             ^
example.java:35: error: ';' expected
                                                        where object_type in (''
VIEW'')
                                                                            ^
example.java:35: error: empty character literal
                                                        where object_type in (''
VIEW'')
                                                                              ^
example.java:35: error: unclosed character literal
                                                        where object_type in (''
VIEW'')
                                                                               ^

example.java:35: error: empty character literal
                                                        where object_type in (''
VIEW'')

    ^
example.java:35: error: not a statement
                                                        where object_type in (''
VIEW'')

 ^
example.java:35: error: unclosed character literal
                                                        where object_type in (''
VIEW'')

     ^
example.java:36: error: empty character literal
                                                        and status = ''INVALID''

                                                                     ^
example.java:36: error: unclosed character literal
                                                        and status = ''INVALID''

                                                                      ^
example.java:36: error: empty character literal
                                                        and status = ''INVALID''

                                                                              ^
example.java:36: error: illegal line end in character literal
                                                        and status = ''INVALID''

                                                                               ^

example.java:39: error: ';' expected
                                                open c1_views;
                                                    ^
example.java:39: error: not a statement
                                                open c1_views;
                                                     ^
example.java:41: error: ';' expected
                                                        fetch c1_views into l_ow
ner, l_object_name;
                                                             ^
example.java:41: error: ';' expected
                                                        fetch c1_views into l_ow
ner, l_object_name;
                                                                           ^
example.java:41: error: not a statement
                                                        fetch c1_views into l_ow
ner, l_object_name;
                                                                            ^
example.java:41: error: ';' expected
                                                        fetch c1_views into l_ow
ner, l_object_name;

   ^
example.java:41: error: not a statement
                                                        fetch c1_views into l_ow
ner, l_object_name;

     ^
example.java:42: error: '(' expected
                                                        if c1_views%notfound the
n
                                                          ^
example.java:42: error: ')' expected
                                                        if c1_views%notfound the
n
                                                                            ^
example.java:43: error: variable declaration not allowed here
                                                                exit;
                                                                ^
example.java:45: error: unclosed character literal
                                                                execute immediat
e 'alter view '||l_owner||'.'||l_object_name||' compile ';

  ^
example.java:45: error: variable declaration not allowed here
                                                                execute immediat
e 'alter view '||l_owner||'.'||l_object_name||' compile ';
                                                                        ^
example.java:45: error: unclosed character literal
                                                                execute immediat
e 'alter view '||l_owner||'.'||l_object_name||' compile ';

              ^
example.java:45: error: unclosed character literal
                                                                execute immediat
e 'alter view '||l_owner||'.'||l_object_name||' compile ';

                                              ^
example.java:45: error: not a statement
                                                                execute immediat
e 'alter view '||l_owner||'.'||l_object_name||' compile ';

                                            ^
example.java:45: error: unclosed character literal
                                                                execute immediat
e 'alter view '||l_owner||'.'||l_object_name||' compile ';

                                                        ^
example.java:45: error: not a statement
                                                                execute immediat
e 'alter view '||l_owner||'.'||l_object_name||' compile ';

                                                ^
example.java:46: error: not a statement
                                                        end if;
                                                        ^
example.java:46: error: ';' expected
                                                        end if;
                                                           ^
example.java:46: error: '(' expected
                                                        end if;
                                                              ^
example.java:46: error: ')' expected
                                                        end if;
                                                               ^
example.java:47: error: not a statement
                                                end loop;
                                                    ^
example.java:49: error: ';' expected
                                                        dbms_utility.compile_sch
ema(schema => p_schema_name);
                                                                    ^
example.java:49: error: illegal start of expression
                                                        dbms_utility.compile_sch
ema(schema => p_schema_name);

            ^
example.java:50: error: not a statement
                                                end;
                                                ^
example.java:51: error: not a statement
                                        end;");
                                        ^
example.java:51: error: unclosed string literal
                                        end;");
                                            ^
51 errors

知道我犯了什么错误吗?

提前致谢。

2 个答案:

答案 0 :(得分:2)

我还建议先在PLSQL中测试你的代码。如果它在那里工作,你可以利用Eclipse的功能,它为你做所有的转义和格式化。

在此处查看已接受的答案:Paste a multi-line Java String in Eclipse

答案 1 :(得分:1)

首先,让PLSQL代码单独使用java。

=>您在代码中不需要''。改为:

                        from dba_objects
                        where object_type in ('VIEW')
                        and status = 'INVALID'
                        and owner = p_schema_name;

然后从错误中,你不能在java字符串中结束行。尝试手动添加EOL,如:

statement = connection.prepareStatement(" declare " + "\n" +
"                        p_schema_name varchar2(400):= upper('" + schema + "');  "+"\n" +
"                        l_owner varchar2(400); "+"\n" +
"                        l_object_name varchar2(400); "+"\n"+ 
"                        cursor c1_views is "+"\n" +
"                            select "+"\n" +
"                                owner, "+"\n"+ 
"                                object_name "+"\n" +
"                            from dba_objects "+"\n" +
"                            where object_type in ('VIEW') "+"\n" + 
"                            and status = 'INVALID' "+"\n" +
"                            and owner = p_schema_name; "+"\n" +
"                    begin "+"\n" +
"                        open c1_views; "+"\n" +
"                        loop "+"\n" +
"                            fetch c1_views into l_owner,l_object_name;  "+"\n" + 
"                            if c1_views%notfound then "+"\n" +
"                                exit; "+"\n" + 
"                            else "+"\n" +
"                                execute immediate 'alter view    '||l_owner||'.'||l_object_name||' compile '; "+"\n" +
"                            end if; "+"\n" +
"                        end loop; "+"\n" +
"                        begin "+"\n" +
"                            dbms_utility.compile_schema(schema =>    p_schema_name); "+"\n" +
"                        end;   "+"\n" +
"                    end;");