How to get foreign key reference with Derby JDBC? - java

I have tables created with
CREATE TABLE COUNTRY (NAME CHAR(16) PRIMARY KEY)
CREATE TABLE PERSON (NAME CAHR(32) PRIMARY KEY,
CITIZENSHIP CHAR(16) CONSTRAINT CY_FK REFERENCES COUNTRY (NAME))
So how can I get Table.Column (COUNTRY.NAME) reference of the foreign key after I've performed SELECT query on PERSON table?
Is it possible to do via JDBC ResultSet, ResultSetMetaData or something alike?
In fact, I need to know:
either column has a Foreign Key constraint;
list of constraint values.
Well, I did SELECT on SYS.SYSCONSTRAINTS but there are only types of constraints ('P', 'F', etc) but no Referenced Tables' names.
Update
I send queries to the Database via JDBC
PreparedStatement stmtDerbyDb = DriverManager.getConnection(dbConnectString).prepareStatement("SELECT * FROM \"" + dbTableOrView + "\"");
ResultSet rsltDerbyDb = stmtDerbyDb.executeQuery();
ResultSetMetaData rsmdDerbyDb = rsltDerbyDb.getMetaData();
...
All the variables stmtDerbyDb, rsltDerbyDb, rsmdDerbyDb are used in further code. I need to know: is it possible to fetch Foreign Key constraints from dbTableOrView via JDBC or shall I query SYS.* system tables somehow?

Possible decision:
...
Connection connTableView = DriverManager.getConnection("jdbc:Derby:database_name:create=false");
DatabaseMetaData dbmdTableView = connTableView.getMetaData();
ResultSet rsltTableConstraints = dbmdTableView.getImportedKeys(null, null, "PERSON");
PreparedStatement prstTableContents = connTableView.prepareStatement("SELECT * FROM PERSON");
ResultSet rsltTableContents = prstTableContents.executeQuery();
...
buildTableView(rsltTableContents);
rsltTableContents.close();
...
while (rsltTableConstraints.next()) {
PreparedStatement prstConstraints = connTableView.prepareStatement("SELECT " + rsltTableConstraints.getString("PKCOLUMN_NAME") + " FROM " + rsltTableConstraints.getString("PKTABLE_NAME"));
ResultSet rsltConstraintsForCol = prstConstraints.execute();
...
setColumnConstraints(rsltConstraintsForCol, rsltTableConstraints.getString("FKCOLUMN_NAME"));
...
rsltConstraintForCol.close();
prstConstraints.close();
}
...
Which is supposed to be used as ComboBox dropdowns with constraints per column (with FK of course).

Related

java.lang.UnsupportedOperationException: DROP CONSTRAINT is only supported for Hibernate

I am trying to drop a foreign key constraint in a table but the SQL "DROP CONSTRAINT" Statement is not supported by ucanaccess. the error says:
java.lang.UnsupportedOperationException: DROP CONSTRAINT is only supported for Hibernate hbm2ddl.auto "create"
Does anybody know a way around this?
private void dropTables() throws SQLException {
final String RENTALS = "RENTALS";
Statement statement = conn.createStatement();
DatabaseMetaData metaData = conn.getMetaData();
ResultSet rs = metaData.getTables(null, null, RENTALS, null);
while (rs.next()) {
if (rs.getString(3).equals(RENTALS)) {
statement.executeUpdate("ALTER TABLE RENTALS DROP CONSTRAINT"
+ " custNumber;");
statement.executeUpdate("ALTER TABLE RENTALS DROP CONSTRAINT"
+ " vehNumber;");
statement.executeUpdate("DROP TABLE CUSTOMERS;");
statement.executeUpdate("DROP TABLE VEHICLE;");
statement.executeUpdate("DROP TABLE RENTALS;");
}
}
}
The error message implies that setting hbm2ddl.auto to create would solve the problem:
<entry key="hibernate.hbm2ddl.auto" value="create">
However, you might want to think twice before doing this, as this means that all the tables associated with your Hibernate entities will be created and dropped each time you start the application.

Java PreparedStatement multiple batch inserts with foreign keys

I have a Java application and am trying to do a series of batch inserts to a MySQL database. However, many of my tables are linked by foreign keys. So I don't know the value of the foreign keys to use because they are generated in a previous batch.
For example take these two tables:
parent
id
name
child
id
parent_id (required foreign key to parent.id)
name
I know how to import these without using batches:
//already initialized variables: Connection connection, List<ParentObject> parentObjects
ResultSet rs = null;
PreparedStatement psParent = null;
PreparedStatement psChild = null;
for(ParentObject parent: parentObjects){
psParent = connection.prepareStatement("INSERT INTO product (name) VALUES (?)", PreparedStatement.RETURN_GENERATED_KEYS);
psParent.setString(1, parent.getName());
psParent.executeUpdate();
int parentId = 0;
rs = psParent.getGeneratedKeys();
if (rs.next())
parentId = rs.getInt(1);
rs.close();
psParent.close();
for(ChildObject child : parent.getChildren()){
psChild = connection.prepareStatement("INSERT INTO child (parent_id, name) VALUES (?,?)");
psChild.setInt(1, parentId);
psChild.setString(2, child.getName());
psChild.executeUpdate();
psChild.close();
}
}
Now I am trying to use batches:
PreparedStatement psParent = connection.prepareStatement("INSERT INTO product (name) VALUES (?)");
PreparedStatement psChild = connection.prepareStatement("INSERT INTO child (parent_id, name) VALUES (?,?)");
for(ParentObject parent: parentObjects){
psParent.setString(1, parent.getName());
psParent.addBatch();
for(ChildObject child : parent.getChildren()){
psChild.setInt(1, I_DONT_KNOW_HOW_TO_GET_THIS_ID_WHICH_HASNT_BEEN_INSERTED_YET);
psChild.setString(2, parent.getName());
psChild.addBatch();
}
}
psParent.executeBatch();
psParent.close();
psChild.executeBatch();
psChild.close();
The actual data structure I am using is much more complicated than what I have above, but it illustrates the basic issue. How would I do the above using batches? The issue I am running into is the child objects cannot be inserted without first knowing the parent_id foreign key to use.
I've searched for answers elsewhere and I cannot find anything to resolve this. I read something about using a stored procedure (which I would like to avoid if possible). Efficiency is important here since I am dealing with potentially millions of records. I appreciate any insight anyone might have.
Don't think it's possible with generated primary key. If your application is only one client for the database maybe you can calculate primary keys by yourself and pass them directly in prepared statements.

Getting foreign key and refering table name of particular database table from metadata DatabaseMetaData using JAVA class

I am writing java class to get all connection database objects(tables). I have used
rs = meta.getExportedKeys(conn.getCatalog(), null, "account_adjustment");
while (rs.next()) {
String fkTableName = rs.getString("FKTABLE_NAME");
String fkColumnName = rs.getString("FKCOLUMN_NAME");
int fkSequence = rs.getInt("KEY_SEQ");
}
which is giving parent table and it's column linked this asked table(account_adjustment)
and also I tried
rs1 = meta.getImportedKeys(conn.getCatalog(), null, "account_adjustment");
while (rs1.next()) {
String fkTableName = rs1.getString("FKTABLE_NAME");
String fkColumnName = rs1.getString("FKCOLUMN_NAME");
int fkSequence = rs1.getInt("KEY_SEQ");
}
which is giving current table(account_adjustment) table and it's foreign key column name
but I want table name with which this foreign key is linked
I have got solution bt in other way not using java,, instead of getting values using java i executed query to 'information_schema' (which is there by default in MySQL) database and 'REFERENTIAL_CONSTRAINTS' table to get all reference of respective tables of required database

How to get super table information in java?

My database had a lot of parent and child tables.The tables contains the foreign key which has the link with the parent table.I wants to get the information of parent table of the child table using java?How can I achieve that?
For ex,consider the student and mark table,
The student table contains the information like studentID,name.
studentID-Primary key
The marks table contains the markId,studentId,Sub1,sub2,sub3 etc
markId-Primarykey
studentID-Foreignkey refers Student table
My table creation queries are,
CREATE TABLE `Student12` (
`studentId` SMALLINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`studentId`)
)
ENGINE = InnoDB;
CREATE TABLE `Marks` (
`markId` SMALLINT NOT NULL AUTO_INCREMENT,
`subject1` SMALLINT NOT NULL,
`subject2` SMALLINT NOT NULL,
`studentId` SMALLINT NOT NULL,
PRIMARY KEY (`markId`),
CONSTRAINT `FK_Marks_Student` FOREIGN KEY `FK_Marks_Student` (`studentId`)
REFERENCES `Student12` (`studentId`)
ON DELETE RESTRICT
ON UPDATE RESTRICT
)
ENGINE = InnoDB;
If I give the mark table name as input, how can I get its parent or super table name student and information about student table?Any help should be appreciable.
It totally depends on the way tables are created. Foreign keys are not mandatory to create, they could be a simple column in one table with no explicit relationship to the other table. If you are very sure that the links are created explicitly (the foreign keys are defined) then you could use information_schema. But if there is no foreign key defined (which is true in most of the databases I have seen), then there is no way for you to find the links inside the database. You have to look into the code (if there is any available) and try to find a clue.
The JDBC DatasetMetaData interface provides a couple of methods that may help. (The following text is copied from the javadoc.
ResultSet getExportedKeys(String catalog, String schema, String table)
Retrieves a description of the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table).
ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, String foreignSchema, String foreignTable)
Retrieves a description of the foreign key columns in the given foreign key table that reference the primary key or the columns representing a unique constraint of the parent table (could be the same or a different table).
Of course, these can only work if the relevant columns have been declared as foreign keys in the SQL table DDL.
You can use the DatabaseMetaData to retrieve informations about foreign keyes
and the referenced Tables. Im not sure if it works with all kinds of MySql Tables.
The principle is to use the follwing code (not tested) to retrieve information about the super tables
ResultSet rs = null;
DatabaseMetaData dm = conn.getMetaData( );
// get super tables of table marks
ResultSet rs = dm.getSuperTables( null , null, "marks" );
while( rs.next( ) ) {
System.out.println(String.format("Table Catalog %s", rs.getString("TABLE_CAT") );
System.out.println(String.format("Table Schema %s", rs.getString("TABLE_SCHEM") );
System.out.println(String.format("Table Name %s", rs.getString("TABLE_NAME") );
System.out.println(String.format("Table Name %s", rs.getString("SUPERTABLE_NAME") );
}
You can use thes informations to get additional informations about the referenced table
and the foreigen and referenced primary keys:
ResultSet rs = dm.getCrossReference( null , null , "student" , null , null , "marks" );
System.out.println(String.format("Exported Keys Info Table %s.", "marks"));
while( rs.next( ) ) {
String pkey = rs.getString("PKCOLUMN_NAME");
String ptab = rs.getString("PKTABLE_NAME");
String fkey = rs.getString("FKCOLUMN_NAME");
String ftab = rs.getString("FKTABLE_NAME");
System.out.println("primary key table = " + ptab);
System.out.println("primary key = " + pkey);
System.out.println("foreign key table = " + ftab);
System.out.println("foreign key = " + fkey);
}
And finally you can retrieve the information about the super table by
ResultSet rs = dm.getTables(null,null,"student" ,null);
System.out.println("Table name:");
while (rs.next()){
String table = rs.getString("TABLE_NAME");
System.out.println(table);
}

Error with foreign key

I'm having problems with creating mySQL table within Java program. I constantly get Can't create table... errno: 150
Here is my code:
String URL="jdbc:mysql://192.168.1.128:3306";
Connection con=(Connection) DriverManager.getConnection(URL,user,pass);
Statement stmt=(Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
Statement stmt1=(Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String mySQL_new_table=("CREATE TABLE IF NOT EXISTS dbtest.T_AJPES_TR " + "(" + "row_count INT PRIMARY KEY AUTO_INCREMENT,"
+ "rn CHAR(15),sSpre CHAR(5),reg CHAR(5),eno VARCHAR(10),davcna VARCHAR(15),Ime VARCHAR(75),Priimek VARCHAR(75),LOG_ID INT,INDEX L_ID (LOG_ID),FOREIGN KEY(LOG_ID) references T_AJPES_TR_LOG(ID_LOG) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = INNODB;");
String mySQL_log = ("CREATE TABLE IF NOT EXISTS dbtest.T_AJPES_TR_LOG" + "(ID_LOG INT PRIMARY KEY AUTO_INCREMENT, Date_import VARCHAR(45),File_import VARCHAR(75)) ENGINE = INNODB;");
stmt.executeUpdate(mySQL_new_table);
stmt1.executeUpdate(mySQL_log);
ResultSet uprs=stmt.executeQuery("SELECT * FROM dbtest.T_AJPES_TR");
ResultSet uprs1=stmt1.executeQuery("SELECT * FROM dbtest.T_AJPES_TR_LOG");
I googled many tutorials for creating foreign key and I still have problems. So what I'm doing wrong?
You've likely got an issue with foreign key constraints, see http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html for more details.
First one to check is whether your LOG_ID reference to T_AJPES_TR_LOG(ID_LOG) is correct

Categories