我正在尝试使用JDBC / MariaDB来尝试实现ER模式
地址更大的图片: http://img854.imageshack.us/img854/2513/psfl.jpg
所以我开始创建Java软件
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.sql.rowset.CachedRowSet;
class DBHandler_test
{
private Statement statement = null;
private Connection connection = null;
private ResultSet result = null;
public DBHandler_test()
{
}
public void createTables() throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
connection = getConnection();
statement = connection.createStatement();
statement.execute("DROP TABLE IF EXISTS Possiede");
statement.execute("DROP TABLE IF EXISTS Possiede2");
statement.execute("DROP TABLE IF EXISTS Possiede3");
statement.execute("DROP TABLE IF EXISTS Camera");
statement.execute("DROP TABLE IF EXISTS Oggetto");
statement.execute("DROP TABLE IF EXISTS Parete");
statement.execute("DROP TABLE IF EXISTS Accesso");
statement.execute("CREATE TABLE IF NOT EXISTS Camera (Nome CHAR(20) PRIMARY KEY, Estensione INT, `Livello_1` CHAR(20), `Livello_2` CHAR(20), `Livello_3` CHAR(20), `Livello_4` CHAR(20))");
statement.execute("CREATE TABLE IF NOT EXISTS Accesso (IdA INTEGER PRIMARY KEY, Coordinata__X INTEGER, Coordinata__Y INTEGER)");
statement.execute("CREATE TABLE IF NOT EXISTS Oggetto (IdO INTEGER PRIMARY KEY, Nome CHAR(20), Coordinata_X INTEGER, Coordinata_Y INTEGER)");
statement.execute("CREATE TABLE IF NOT EXISTS Parete (IdP INTEGER PRIMARY KEY, Coordinata_X CHAR(10), Coordinata_Y CHAR(10))");
statement.execute("CREATE TABLE IF NOT EXISTS Possiede (IdO INTEGER NOT NULL, Nome CHAR(20), PRIMARY KEY (IdO), FOREIGN KEY (Nome) REFERENCES Camera(Nome), FOREIGN KEY (IdO) REFERENCES Oggetto(IdO))");
statement.execute("CREATE TABLE IF NOT EXISTS Possiede2 (IdA INTEGER NOT NULL, Nome CHAR(20) NOT NULL, Coordinata_X INTEGER, Coordinata_Y INTEGER, PRIMARY KEY (IdA,Nome), FOREIGN KEY (IdA) REFERENCES Accesso(IdA), FOREIGN KEY (Nome) REFERENCES Camera(Nome))");
statement.execute("CREATE TABLE IF NOT EXISTS Possiede3 (IdP INTEGER NOT NULL, Nome CHAR(20) NOT NULL, PRIMARY KEY (IdP, Nome), FOREIGN KEY (Nome) REFERENCES Camera(Nome), FOREIGN KEY (IdP) REFERENCES Parete(IdP))");
connection.close();
}
public void insertRoomsEntries() throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
connection = getConnection();
statement = connection.createStatement();
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (1,'Cappelliera',3,2)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (2,'Attaccapanni',6,5)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (3,'Letto matrimoniale',3,10)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (4,'Comodino',3,9)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (5,'Comodino',3,12)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (6,'Armadio',7,3)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (7,'Bidet',8,2)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (8,'Water',10,2)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (9,'Lavandino',9,4)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (10,'Doccia',12,3)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (11,'Televisione',14,4)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (12,'Divano',17,5)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (13,'Tavolo',20,3)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (14,'Frigo',21,8)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (15,'Lavello',19,12)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (16,'Cestino',16,8)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (17,'Letto',8,9)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (18,'Comodino',7,8)");
statement.execute("INSERT INTO Oggetto(IdO, Nome, Coordinata_X, Coordinata_Y) VALUES (19,'Cassettiera',11,8)");
statement.execute("INSERT INTO Camera VALUES ('Ingresso', 28, 'Ingresso', 'NULL', 'NULL', 'NULL')");
statement.execute("INSERT INTO Camera VALUES ('Camera da letto 1', 20, 'Ingresso', 'Camera da letto 1', 'NULL', 'NULL')");
statement.execute("INSERT INTO Camera VALUES ('Bagno', 18, 'Ingresso', 'Corridoio 1', 'Bagno', 'NULL')");
statement.execute("INSERT INTO Camera VALUES ('Corridoio 1', 20, 'Ingresso', 'Corridoio 1', 'NULL', 'NULL')");
statement.execute("INSERT INTO Camera VALUES ('Salotto', 48, 'Ingresso', 'Corridoio 1', 'NULL', 'NULL')");
statement.execute("INSERT INTO Camera VALUES ('Camera da letto 2', 15, 'Ingresso', 'Corridoio 2', 'Camera da letto 2', 'NULL')");
statement.execute("INSERT INTO Camera VALUES ('Ripostiglio', 12, 'Ingresso', 'Corridoio 2', 'Ripostiglio', 'NULL')");
statement.execute("INSERT INTO Camera VALUES ('Cucina', 30, 'Ingresso', 'Corridoio 1', 'Salotto', 'Cucina')");
statement.execute("INSERT INTO Accesso VALUES (1, 6, 5)");
statement.execute("INSERT INTO Accesso VALUES (2, 6, 7)");
statement.execute("INSERT INTO Accesso VALUES (3, 11, 4)");
statement.execute("INSERT INTO Accesso VALUES (4, 13, 5)");
statement.execute("INSERT INTO Accesso VALUES (5, 18, 7)");
statement.execute("INSERT INTO Accesso VALUES (6, 6, 12)");
statement.execute("INSERT INTO Accesso VALUES (7, 10, 11)");
statement.execute("INSERT INTO Accesso VALUES (8, 14, 12)");
connection.close();
}
public static Connection getConnection()
throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("/home/caterpillar/workspace/BDC_esercitazione/src/database.properties");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
Class.forName(drivers).newInstance();
return DriverManager.getConnection(url, username, password);
}
public static void main(String[] args)
{
DBHandler_test dbHandler = new DBHandler_test();
try
{
dbHandler.createTables();
dbHandler.insertRoomsEntries();
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
但是当我在PHPmyadmin中打开Possiede2表时,我看到它是空的。这怎么可能? Camera和Accesso表不为空。我在SQL命令中犯了什么错误吗?
答案 0 :(得分:0)
我认为引用键会让Camera和Accesso出现在Possiede2中,但似乎不是。所以我必须手动输入条目,小心选择Camera和Accesso表中的条目。 SQL代码的一个示例:
INSERT INTO Possiede2 VALUES (6, 'Ingresso', 6, 12)