Derby Data Base Updation through java?-Collection of common programming errors
I had this code copied from the net and made some required changes, for my data base
public class MainClass {
static Connection conn;
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println(“Usage: java JavaDBDemo “); System.exit(1); }
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName = "JeeteshBD";
String connectionURL = "jdbc:derby:" + dbName + ";";
String createString = "CREATE TABLE SANKYA.ADDRESS (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
Class.forName(driver);
conn = DriverManager.getConnection(connectionURL);
Statement stmt = conn.createStatement();
stmt.setPoolable(true);
stmt.execute(createString);
PreparedStatement psInsert = conn
.prepareStatement("insert into SANKYA.ADDRESS values (?,?)");
psInsert.setString(1, args[0]);
psInsert.setString(2, args[1]);
psInsert.executeUpdate();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt2.executeQuery("select * from SANKYA.ADDRESS");
System.out.println("Addressed present in your Address Book\n\n");
int num = 0;
while (rs.next()) {
System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address"
+ rs.getString(2));
}
rs.close();
} }
After executing the following code I get following error in the console.
Exception in thread "main" java.sql.SQLException: Table/View 'ADDRESS' already exists in Schema 'SANKYA'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at MainClass.main(MainClass.java:26)
Caused by: java.sql.SQLException: Table/View ‘ADDRESS’ already exists in Schema ‘SANKYA’. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) … 10 more Caused by: ERROR X0Y32: Table/View ‘ADDRESS’ already exists in Schema ‘SANKYA’. at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.duplicateDescriptorException(Unknown Source) at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.addDescriptor(Unknown Source) at org.apache.derby.impl.sql.execute.CreateTableConstantAction.executeConstantAction(Unknown Source) at org.apache.derby.impl.sql.execute.MiscResultSet.open(Unknown Source) at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source) at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source) … 4 more
But when i look at the data base perspective in Eclipse, the updation is not done, can anyone throw some light upon this?
This is a derby.log that is created on the desktop
----------------------------------------------------------------
2013-03-11 05:10:17.241 GMT: Booting Derby version The Apache Software Foundation – Apache Derby – 10.5.1.1 – (764942): instance c013800d-013d-57db-3673-0000018a21b0 on database directory /home/sankya/JeeteshBD
Database Class Loader started – derby.database.classpath=”
2013-03-11 05:10:17.639 GMT:
Shutting down instance c013800d-013d-57db-3673-0000018a21b0
2013-03-11 05:10:27.773 GMT: Booting Derby version The Apache Software Foundation – Apache Derby – 10.5.1.1 – (764942): instance a816c00e-013d-57db-3673-0000018a21b0 on database directory /home/sankya/JeeteshBD
Database Class Loader started – derby.database.classpath=”
2013-03-11 06:22:45.639 GMT:
Shutting down instance a816c00e-013d-57db-3673-0000018a21b0
-
You need to create table only if doesn’t exist.
Connection connection = DriverManager.getConnection(connectionURL); DatabaseMetaData dbmd = connection.getMetaData(); ResultSet rs = dbmd.getTables(null, "SANKYA", "ADDRESS", null); if(!rs.next()) { Statement stmt = conn.createStatement(); stmt.setPoolable(true); stmt.execute(createString); }
-
stmt.execute(createString);
The Above line give sql exception. so next line not able to execute. It’s clear mention that the table exists in your DB. Check your DB properly..
If table exists in your table the comment the “
stmt.execute(createString);
” this .. Run the application it’s work surely ..
Originally posted 2013-11-09 23:35:48.