1
我想在我的學校項目中使用德比,但我有一些問題。 我的數據庫名爲theaterDB,derby 10.2.1,JDBC 3.0.0Derby和SQL請求問題:如何確定默認模式?
我通過java創建的每個SQL請求都不成功......我無法理解爲什麼。 例如,請求: SELECT * FROM User
返回異常:
java.sql.SQLSyntaxErrorException: Schema 'ADMIN' does not exist
不工作的另一個SQL:
ALTER table theaterDB."Projection" ADD INDEX(fk_Projection_Movie(Movie_id));
Syntax error: Encountered "" at line 1, column 45.
這裏是java:
public Connection getConnection()
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
System.out.println("Connection to: jdbc:derby:C:\\Users\\acouty\\theaterDB;create=true");
// DriverManager.get
return DriverManager.getConnection("jdbc:derby:C:\\Users\\acouty\\theaterDB;create=true", "admin", "");
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
}
public List<User> listUsers()
{
final ArrayList<User> users = new ArrayList<User>();
System.out.println("List user request");
try
{
final Connection connection = getConnection();
final String query = "SELECT * FROM User";
System.out.println("query is : " + query);
final ResultSet rs = connection.createStatement().executeQuery(query);
while (rs.next())
{
System.out.println("salut");
}
return null;
}
catch (final Exception e)
{
e.printStackTrace();
}
return null;
}
這裏是.SQL:
CREATE TABLE theaterDB."Users"
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name VARCHAR(45) NOT NULL,
lastname VARCHAR(45) NOT NULL ,
email VARCHAR(45) NOT NULL ,
adress VARCHAR(45) NOT NULL ,
city VARCHAR(45) NOT NULL ,
zip VARCHAR(45) NOT NULL ,
login VARCHAR(45) NOT NULL ,
password VARCHAR(45) NOT NULL ,
admin SMALLINT NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
CREATE TABLE theaterDB."Movie"
(
id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
title VARCHAR(45) NOT NULL ,
resume VARCHAR(500) ,
genre VARCHAR(60) ,
grade INT ,
review_pub VARCHAR(200) ,
review_gen VARCHAR(200) ,
poster VARCHAR(100) ,
duration INT ,
release_date VARCHAR(45) ,
PRIMARY KEY (id)
);
CREATE TABLE theaterDB."Projection" (
id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
date DATE ,
length INT ,
Movie_id INT NOT NULL ,
price DECIMAL(10,0) ,
location VARCHAR(45) NOT NULL ,
place_nbr INT NOT NULL ,
PRIMARY KEY (id)
);
ALTER TABLE theaterDB."Projection"
ADD FOREIGN KEY(Movie_id)
REFERENCES theaterDB."Movie" (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
CREATE TABLE theaterDB."command"
(
id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
Projection_id INT NOT NULL ,
User_id INT NOT NULL ,
paid SMALLINT NOT NULL ,
PRIMARY KEY (id)
);
ALTER TABLE theaterDB."command"
ADD FOREIGN KEY(Projection_id)
REFERENCES theaterDB."Projection" (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
ALTER TABLE theaterDB."command"
ADD FOREIGN KEY(User_id)
REFERENCES theaterDB."Users" (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
謝謝
也許你需要模式名稱,如select * from myStuff.User; –
嗨,諾普,我試了theaterDB.User,theaterDB.'User'...我添加其他查詢不起作用... – ogdabou
'用戶'是一個保留的關鍵字,需要特殊待遇。我建議你將表格重命名爲USERS。 –