{"id":1175,"date":"2022-08-30T15:13:38","date_gmt":"2022-08-30T15:13:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/derby-data-base-updation-through-java-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:38","modified_gmt":"2022-08-30T15:13:38","slug":"derby-data-base-updation-through-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/derby-data-base-updation-through-java-collection-of-common-programming-errors\/","title":{"rendered":"Derby Data Base Updation through java?-Collection of common programming errors"},"content":{"rendered":"<p><strong>I had this code copied from the net and made some required changes, for my data base<\/strong><\/p>\n<pre><code>public class MainClass {\n<\/code><\/pre>\n<p>static Connection conn;<\/p>\n<p>public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println(&#8220;Usage: java JavaDBDemo &#8220;); System.exit(1); }<\/p>\n<pre><code>String driver = \"org.apache.derby.jdbc.EmbeddedDriver\";\nString dbName = \"JeeteshBD\";\nString connectionURL = \"jdbc:derby:\" + dbName + \";\";\nString createString = \"CREATE TABLE SANKYA.ADDRESS (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)\";\nClass.forName(driver);\n\nconn = DriverManager.getConnection(connectionURL);\n\nStatement stmt = conn.createStatement();\nstmt.setPoolable(true);\nstmt.execute(createString);\n\n\nPreparedStatement psInsert = conn\n    .prepareStatement(\"insert into SANKYA.ADDRESS values (?,?)\");\n\npsInsert.setString(1, args[0]);\npsInsert.setString(2, args[1]);\n\npsInsert.executeUpdate();\n\nStatement stmt2 = conn.createStatement();\nResultSet rs = stmt2.executeQuery(\"select * from SANKYA.ADDRESS\");\nSystem.out.println(\"Addressed present in your Address Book\\n\\n\");\nint num = 0;\n\nwhile (rs.next()) {\n  System.out.println(++num + \": Name: \" + rs.getString(1) + \"\\n Address\"\n      + rs.getString(2));\n}\nrs.close();\n<\/code><\/pre>\n<p>} }<\/p>\n<p><strong>After executing the following code I get following error in the console.<\/strong><\/p>\n<pre><code>Exception in thread \"main\" java.sql.SQLException: Table\/View 'ADDRESS' already exists in Schema 'SANKYA'.\nat org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)\nat org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)\nat org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)\nat org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)\nat org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)\nat org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)\nat org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)\nat org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)\nat org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)\nat MainClass.main(MainClass.java:26)\n<\/code><\/pre>\n<p>Caused by: java.sql.SQLException: Table\/View &#8216;ADDRESS&#8217; already exists in Schema &#8216;SANKYA&#8217;. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) &#8230; 10 more Caused by: ERROR X0Y32: Table\/View &#8216;ADDRESS&#8217; already exists in Schema &#8216;SANKYA&#8217;. 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) &#8230; 4 more<\/p>\n<p><strong>But when i look at the data base perspective in Eclipse, the updation is not done, can anyone throw some light upon this?<\/strong><\/p>\n<p><strong>This is a derby.log that is created on the desktop<\/strong><\/p>\n<pre><code>----------------------------------------------------------------\n<\/code><\/pre>\n<p>2013-03-11 05:10:17.241 GMT: Booting Derby version The Apache Software Foundation &#8211; Apache Derby &#8211; 10.5.1.1 &#8211; (764942): instance c013800d-013d-57db-3673-0000018a21b0 on database directory \/home\/sankya\/JeeteshBD<\/p>\n<p>Database Class Loader started &#8211; derby.database.classpath=&#8221;<\/p>\n<p>2013-03-11 05:10:17.639 GMT:<\/p>\n<h2>Shutting down instance c013800d-013d-57db-3673-0000018a21b0<\/h2>\n<p>2013-03-11 05:10:27.773 GMT: Booting Derby version The Apache Software Foundation &#8211; Apache Derby &#8211; 10.5.1.1 &#8211; (764942): instance a816c00e-013d-57db-3673-0000018a21b0 on database directory \/home\/sankya\/JeeteshBD<\/p>\n<p>Database Class Loader started &#8211; derby.database.classpath=&#8221;<\/p>\n<p>2013-03-11 06:22:45.639 GMT:<\/p>\n<h2>Shutting down instance a816c00e-013d-57db-3673-0000018a21b0<\/h2>\n<ol>\n<li>\n<p>You need to create table only if doesn&#8217;t exist.<\/p>\n<pre><code>Connection connection = DriverManager.getConnection(connectionURL);\nDatabaseMetaData dbmd = connection.getMetaData();\nResultSet rs = dbmd.getTables(null, \"SANKYA\", \"ADDRESS\", null);\nif(!rs.next())\n{\n    Statement stmt = conn.createStatement();\n    stmt.setPoolable(true);\n    stmt.execute(createString);\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>stmt.execute(createString); \n<\/code><\/pre>\n<p>The Above line give sql exception. so next line not able to execute. It&#8217;s clear mention that the table exists in your DB. Check your DB properly..<\/p>\n<p>If table exists in your table the comment the &#8220;<code>stmt.execute(createString);<\/code>&#8221; this .. Run the application it&#8217;s work surely ..<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:35:48. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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(&#8220;Usage: java JavaDBDemo &#8220;); System.exit(1); } String driver = &#8220;org.apache.derby.jdbc.EmbeddedDriver&#8221;; String dbName = &#8220;JeeteshBD&#8221;; String connectionURL = &#8220;jdbc:derby:&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1175","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1175"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}