I have a java file that connects to a database and this is the code:
package movies;
import java.sql.*;
public class CreateTable {
public static void main(String args[]) {
Connection c = null;
Statement stmt = null;
String sql;
try {
Class.forName("oracle.jdbc.OracleDriver");//driver
c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE", "username", "password");//PUT DATABASE CONNECTION INFO
System.out.println("Opened database successfully from within CreateTable.java");
stmt = c.createStatement();
sql = "CREATE TABLE MOVIES "
//+ "(ID SERIAL PRIMARY KEY,"//can't figure out this statement and it works if I comment it out.
+ " (NAME NVARCHAR2(255) PRIMARY KEY,"//I don't want this to be the primary key.
+ " YEAR NVARCHAR2(255),"
+ " RATING NVARCHAR2(16),"
+ " ACTORS NVARCHAR2(1024))";
stmt.executeUpdate(sql);
stmt.close();
c.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Table created successfully");
}
}
The commented part is where I'm running into problems. I'm trying to convert a PostgreSQL statement to an Oracle 11g XE statement. I would like to have an ID with a sequential number to identify it. (ie. 0001, 0002, 0003, etc.) How can I do this? Could you provide an example? I'm at a loss right now. I'm using Netbeans 8.02, Oracle 11g XE, and Apache Tomcat 8.0.15.0.
edit I'm trying to have the ID column as the only primary key. I would eliminate the NAME column as a primary key and make the ID column the new primary key.
I don't think you can have two separate columns with primary key like that, if you want a primary key on two columns use,
edit :
sql = "CREATE TABLE MOVIES "
+ "(ID INT PRIMARY KEY," -- CHANGES
+ " NAME NVARCHAR2(255),"
+ " YEAR NVARCHAR2(255),"
+ " RATING NVARCHAR2(16),"
+ " ACTORS NVARCHAR2(1024))";
i have created the table to show the syntax works in oracle now..
SQL> create table movies
2 (id int primary key,
3 name NVARCHAR2(255),
4 year NVARCHAR2(255),
5 rating NVARCHAR2(255),
6 actors NVARCHAR2(1024));
Table created.
This code worked. See the comments below to view the changes:
try {
Class.forName("oracle.jdbc.OracleDriver");//driver
c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE", "username", "password");//PUT DATABASE CONNECTION INFO
System.out.println("Opened database successfully from within CreateTable.java");
stmt = c.createStatement();
sql = "CREATE TABLE MOVIES "
//THE FOLLOWING LINE WAS CHANGED:
+ "(\"ID\" INTEGER PRIMARY KEY, \"TYPE\" VARCHAR2(32),"
+ " NAME NVARCHAR2(255),"
+ " YEAR NVARCHAR2(255),"
+ " RATING NVARCHAR2(16),"
+ " ACTORS NVARCHAR2(1024))";
//THE FOLLOW TWO STATEMENTS WERE ADDED:
String sql1 = "CREATE SEQUENCE MOVIES_SEQ START WITH 1 INCREMENT BY 1 NOMAXVALUE";
String sql2 = "CREATE TRIGGER MOVIES_TRIGGER BEFORE INSERT ON MOVIES FOR EACH ROW BEGIN SELECT MOVIES_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL; END;";
stmt.executeUpdate(sql);
//THESE TWO WERE ALSO ADDED:
stmt.executeUpdate(sql1);
stmt.executeUpdate(sql2);
stmt.close();
c.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.