获取SQL语法错误异常,不知道为什么?

时间:2015-03-15 18:10:41

标签: java sql database oracle jdbc

我使用下面的代码片段来计算数据库上的一些范围查询。目前,当我运行代码时,我收到以下错误。

SQLSyntaxErrorException: ORA-00955: name is already used by an existing object

任何人都知道可能导致这种情况的原因是什么?我是数据库专门用于JDBC的新手,任何帮助都将受到赞赏。

    String mainCity = "Main_City";

    String view_Main_City = "CREATE VIEW "+mainCity+"(City, Phi,Lamda) AS "
            +"SELECT CITY, ((Latitude*3.14)/180), ((Longitude*3.14)/180)"
            +"FROM MAP "
            + "WHERE CITY = " + "'" 
            + args[3]+"'";


    String otherCities = "OtherCities";

    String view_Without_Main_City = "CREATE VIEW "+otherCities+"(City, Phi,Lamda) AS "
            +"SELECT CITY, ((Latitude*3.14)/180), ((Longitude*3.14)/180)"
            +"FROM MAP "
            + "WHERE CITY <> " + "'" 
            + args[3]+"'";


    String HaverViewName = "Haver";

    String Haver = "CREATE VIEW "+ HaverViewName +"(City,hav) AS "
            + "SELECT O.City,(  (SIN((M.PHI - O.PHI)/2) * SIN((M.PHI - O.PHI)/2))+COS(M.PHI)*COS(O.PHI)*(SIN((M.LAMDA - O.LAMDA)/2)*SIN((M.LAMDA - O.LAMDA)/2)) ) "
            + "FROM Main_City M,OtherCities O";


    String Distance_From_Main_City = "Distance_From_Main_City";

    String d_f_m = "CREATE VIEW "+Distance_From_Main_City+"(CITY,Distance) AS "
            + "SELECT O.City , ( 12742 * aTAN2((SQRT(H.HAV)), (SQRT(1-H.HAV))) ) AS d "
            + "FROM OtherCities O JOIN Haver H "
            + "ON O.City = H.City";

    ResultSet m = stmt.executeQuery(view_Main_City);
    ResultSet v = stmt.executeQuery(view_Without_Main_City);
    ResultSet h = stmt.executeQuery(Haver);
    ResultSet d = stmt.executeQuery(d_f_m);


    if(args[2].equals("RANGE_QUERY")){

        String Range_Q = "SELECT CITY, ROUND(Distance,2) AS Distance "
                + "FROM Distance_From_Main_City "
                +"WHERE Distance <" + args[4]
                        + " ORDER BY DISTANCE ASC";

        ResultSet Range_Query_Answer = stmt.executeQuery(Range_Q);

        while(Range_Query_Answer.next()){
            System.out.println(Range_Query_Answer.getString("City") + " " + Range_Query_Answer.getString("Distance"));
        }
    }

2 个答案:

答案 0 :(得分:1)

好吧,似乎您要使用的其中一个视图名称已被表或其他视图使用。因此,请检查重复项(还要检查是否在第二次运行应用程序之前删除了现有视图)。

答案 1 :(得分:1)

这些观点似乎已经存在。

您想使用CREATE OR REPLACE VIEW代替CREATE VIEW。根据关键字的建议,如果视图不存在,则会创建视图,如果视图确实存在则会更改视图。