java.sql.SQLSyntaxErrorException:ORA-00955:name已被现有对象使用

时间:2013-01-30 11:30:52

标签: sql oracle servlets

我正在使用Oracle 11g R2,我想创建一些用户表。当我运行查询。它创建了几个表并生成了 java.sql.SQLSyntaxErrorException:ORA-00955:现有对象已经使用了名称异常。

    Connection con=prepareConnection();
    Statement st=con.createStatement();
    StringBuilder sb=new StringBuilder(1024);
    sb.append("create table ").append(uname).append("(MESSAGES CLOB,LINKS VARCHAR2(150),FRIENDS VARCHAR2(50),COMMENTS CLOB,LIKES VARCHAR2(10),UNLIKES VARCHAR2(10),SHARES BLOB,QSTNS CLOB,ANS CLOB,UPDATES BLOB,THEMS VARCHAR2(100),WORDS CLOB,NOTIFICATION CLOB,REQUESTS VARCHAR2(100),TIPS CLOB,TAG VARCHAR2(50))");
    String Query=sb.toString();
    st.executeUpdate(Query);

    Statement st1=con.createStatement();
    StringBuilder sb1=new StringBuilder(1024);
    sb1.append("create table ").append(uname).append("VIALBUM(ALBUMID NUMBER NOT NULL, ALBUMNAME VARCHAR2(225) NOT NULL, CONSTRAINT ").append(uname).append("VIALBUM_PK PRIMARY KEY(ALBUMID)ENABLE)");
    String Query1=sb1.toString();
    st1.executeUpdate(Query1);

    Statement st12=con.createStatement();
    StringBuilder sb12=new StringBuilder(1024);
    sb12.append("CREATE INDEX ").append(uname).append("VIALBUM_INDEX ON ").append(uname).append("VIALBUM(ALBUMNAME)");
    String Query12=sb12.toString();
    st12.executeUpdate(Query12);

    Statement st11=con.createStatement();
    StringBuilder sb11=new StringBuilder(1024);
    sb11.append("create table ").append(uname).append("VIDEO(VIDEOID NUMBER NOT NULL, VIDEONAME VARCHAR2(225) NOT NULL, VIDEOFULL BLOB NOT NULL, VIDEODISC VARCHAR2(225), ALBUMID NUMBER NOT NULL, CONSTRAINT ").append(uname).append("VIDEO_PK PRIMARY KEY(VIDEOID)ENABLE)");
    String Query11=sb11.toString();
    st11.executeUpdate(Query11);

    Statement st13=con.createStatement();
    StringBuilder sb13=new StringBuilder(1024);
    sb13.append("ALTER TABLE ").append(uname).append("VIDEO ADD CONSTRAINT ").append(uname).append("VIDEO_").append(uname).append("VIALBUM_FK1 FOREIGN KEY(ALBUMID)REFERENCES ").append(uname).append("VIALBUM(ALBUMID)ENABLE");
    String Query13=sb13.toString();
    st13.executeUpdate(Query13);

    Statement st14=con.createStatement();
    StringBuilder sb14=new StringBuilder(1024);
    sb14.append("CREATE INDEX ").append(uname).append("VIDEO_INDEX ON ").append(uname).append("VIDEO (VIDEONAME)");
    String Query14=sb14.toString();
    st14.executeUpdate(Query14);

    Statement st2=con.createStatement();
    StringBuilder sb2=new StringBuilder(1024);
    sb2.append("create table ").append(uname).append("ALBUM(ALBUMID NUMBER NOT NULL, ALBUMNAME VARCHAR2(225) NOT NULL, CONSTRAINT ").append(uname).append("ALBUM_PK PRIMARY KEY(ALBUMID)ENABLE)");
    String Query2=sb2.toString();
    st2.executeUpdate(Query2);

    Statement st22=con.createStatement();
    StringBuilder sb22=new StringBuilder(1024);
    sb22.append("CREATE INDEX ").append(uname).append("ALBUM_INDEX ON ").append(uname).append("ALBUM(ALBUMNAME)");
    String Query22=sb22.toString();
    st22.executeUpdate(Query22);

直到上面的语句才能正确执行。其余的语句不会被执行。它会抛出异常。

    Statement st21=con.createStatement();
    StringBuilder sb21=new StringBuilder(1024);
    sb21.append("create table ").append(uname).append("IMAGE(IMAGEID NUMBER NOT NULL, IMAGENAME VARCHAR2(225) NOT NULL, IMAGEFULL BLOB NOT NULL, IMAGEDISC VARCHAR2(225), ALBUMID NUMBER NOT NULL, CONSTRAINT ").append(uname).append("IMAGE_PK PRIMARY KEY(IMAGEID)ENABLE)");
    String Query21=sb11.toString();
    st21.executeUpdate(Query21);

    Statement st23=con.createStatement();
    StringBuilder sb23=new StringBuilder(1024);
    sb23.append("ALTER TABLE ").append(uname).append("IMAGE ADD CONSTRAINT ").append(uname).append("IMAGE_").append(uname).append("ALBUM_FK1 FOREIGN KEY(ALBUMID)REFERENCES ").append(uname).append("ALBUM(ALBUMID)ENABLE");
    String Query23=sb23.toString();
    st23.executeUpdate(Query23);

    Statement st24=con.createStatement();
    StringBuilder sb24=new StringBuilder(1024);
    sb24.append("CREATE INDEX ").append(uname).append("IMAGE_INDEX ON ").append(uname).append("IMAGE (IMAGENAME)");
    String Query24=sb24.toString();
    st24.executeUpdate(Query24);

    Statement st3=con.createStatement();
    StringBuilder sb3=new StringBuilder(1024);
    sb3.append("create table ").append(uname).append("MUALBUM(ALBUMID NUMBER NOT NULL, ALBUMNAME VARCHAR2(225) NOT NULL, CONSTRAINT ").append(uname).append("MUALBUM_PK PRIMARY KEY(ALBUMID)ENABLE)");
    String Query3=sb3.toString();
    st3.executeUpdate(Query3);

    Statement st32=con.createStatement();
    StringBuilder sb32=new StringBuilder(1024);
    sb32.append("CREATE INDEX ").append(uname).append("MUALBUM_INDEX ON ").append(uname).append("MUALBUM(ALBUMNAME)");
    String Query32=sb12.toString();
    st32.executeUpdate(Query32);

    Statement st31=con.createStatement();
    StringBuilder sb31=new StringBuilder(1024);
    sb31.append("create table ").append(uname).append("SONG(SONGID NUMBER NOT NULL, SONGNAME VARCHAR2(225) NOT NULL, SONGFULL BLOB NOT NULL, SONGDISC VARCHAR2(225), ALBUMID NUMBER NOT NULL, CONSTRAINT ").append(uname).append("SONG_PK PRIMARY KEY(SONGID)ENABLE)");
    String Query31=sb31.toString();
    st31.executeUpdate(Query31);

    Statement st33=con.createStatement();
    StringBuilder sb33=new StringBuilder(1024);
    sb33.append("ALTER TABLE ").append(uname).append("SONG ADD CONSTRAINT ").append(uname).append("SONG_").append(uname).append("ALBUM_FK1 FOREIGN KEY(ALBUMID)REFERENCES ").append(uname).append("ALBUM(ALBUMID)ENABLE");
    String Query33=sb33.toString();
    st33.executeUpdate(Query33);

    Statement st34=con.createStatement();
    StringBuilder sb34=new StringBuilder(1024);
    sb34.append("CREATE INDEX ").append(uname).append("SONG_INDEX ON ").append(uname).append("SONG (SONGNAME)");
    String Query34=sb34.toString();
    st34.executeUpdate(Query34);

请任何人告诉我这个错误告诉我什么。

2 个答案:

答案 0 :(得分:2)

当您执行此代码String Query21=sb11.toString();时,您尝试创建一个已在上面创建的表。

也许这些String Query21=sb21.toString();可以为您省去一些麻烦

答案 1 :(得分:1)

听起来你正试图创建一个与另一个同名的对象 - 例如表格或约束,字段等。

看起来你曾试图在这里创建两次相同的表:

String Query21=sb11.toString();