{"id":428,"date":"2007-10-28T00:00:00","date_gmt":"2007-10-27T16:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=428"},"modified":"2011-07-15T09:56:59","modified_gmt":"2011-07-15T01:56:59","slug":"connect-to-java-db-derby-with-org-apache-derby-jdbc-embeddeddriver","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=428","title":{"rendered":"Connect to Java DB (Derby) with org.apache.derby.jdbc.EmbeddedDriver"},"content":{"rendered":"<p><FONT face=\"Courier New\"><FONT color=#7f0055><STRONG>import&nbsp;<\/STRONG><\/FONT><FONT color=#000000>java.sql.Connection;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>java.sql.DriverManager;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>java.sql.PreparedStatement;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>java.sql.ResultSet;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>java.sql.Statement;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#7f0055><B>public&nbsp;class&nbsp;<\/B><\/FONT><FONT color=#000000>JavaDBDemo&nbsp;<\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>static&nbsp;<\/B><\/FONT><FONT color=#000000>Connection&nbsp;conn;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;static&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>main<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>String<\/FONT><FONT color=#000000>[]&nbsp;<\/FONT><FONT color=#000000>args<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>String&nbsp;driver&nbsp;=&nbsp;<\/FONT><FONT color=#2a00ff>&#8220;org.apache.derby.jdbc.EmbeddedDriver&#8221;<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>String&nbsp;connectionURL&nbsp;=&nbsp;<\/FONT><FONT color=#2a00ff>&#8220;jdbc:derby:myDatabase;create=true&#8221;<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>String&nbsp;createString&nbsp;=&nbsp;<\/FONT><FONT color=#2a00ff>&#8220;CREATE&nbsp;TABLE&nbsp;Employee&nbsp;(NAME&nbsp;VARCHAR(32)&nbsp;NOT&nbsp;NULL,&nbsp;ADDRESS&nbsp;VARCHAR(50)&nbsp;NOT&nbsp;NULL)&#8221;<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>try&nbsp;<\/B><\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>Class<\/B><\/FONT><FONT color=#000000>.forName<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>driver<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}&nbsp;<\/FONT><FONT color=#7f0055><B>catch&nbsp;<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>java.lang.ClassNotFoundException&nbsp;e<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>e.printStackTrace<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>try&nbsp;<\/B><\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>conn&nbsp;=&nbsp;DriverManager.getConnection<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>connectionURL<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>Statement&nbsp;stmt&nbsp;=&nbsp;conn.createStatement<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>stmt.executeUpdate<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>createString<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>PreparedStatement&nbsp;psInsert&nbsp;=&nbsp;conn.prepareStatement<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;insert&nbsp;into&nbsp;Employee&nbsp;values&nbsp;(?,?)&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>psInsert.setString<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#990000>1<\/FONT><FONT color=#000000>,&nbsp;args<\/FONT><FONT color=#000000>[<\/FONT><FONT color=#990000>0<\/FONT><FONT color=#000000>])<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>psInsert.setString<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#990000>2<\/FONT><FONT color=#000000>,&nbsp;args<\/FONT><FONT color=#000000>[<\/FONT><FONT color=#990000>1<\/FONT><FONT color=#000000>])<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>psInsert.executeUpdate<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>Statement&nbsp;stmt2&nbsp;=&nbsp;conn.createStatement<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>ResultSet&nbsp;rs&nbsp;=&nbsp;stmt2.executeQuery<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;select&nbsp;*&nbsp;from&nbsp;Employee&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>int&nbsp;<\/B><\/FONT><FONT color=#000000>num&nbsp;=&nbsp;<\/FONT><FONT color=#990000>0<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>while&nbsp;<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>rs.next<\/FONT><FONT color=#000000>())&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>++num&nbsp;+&nbsp;<\/FONT><FONT color=#2a00ff>&#8220;:&nbsp;Name:&nbsp;&#8221;&nbsp;<\/FONT><FONT color=#000000>+&nbsp;rs.getString<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#990000>1<\/FONT><FONT color=#000000>)&nbsp;<\/FONT><FONT color=#000000>+&nbsp;<\/FONT><FONT color=#2a00ff>&#8220;\\n&nbsp;Address&#8221;&nbsp;<\/FONT><FONT color=#000000>+&nbsp;rs.getString<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#990000>2<\/FONT><FONT color=#000000>))<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>rs.close<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}&nbsp;<\/FONT><FONT color=#7f0055><B>catch&nbsp;<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>Exception&nbsp;e<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>e.printStackTrace<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#000000>}<\/FONT><\/FONT><BR><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import&nbsp;java.sql.Connection;import&nbsp;java.sql.DriverManager;import&nbsp;java.sql.PreparedStatement;import&nbsp;java.sql.ResultSet;import&nbsp;java.sql.Statement;public&nbsp;class&nbsp;JavaDBDemo&nbsp;{&nbsp;&nbsp;static&nbsp;Connection&nbsp;conn;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;driver&nbsp;=&nbsp;&#8220;org.apache.derby.jdbc.EmbeddedDriver&#8221;;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;connectionURL&nbsp;=&nbsp;&#8220;jdbc:derby:myDatabase;create=true&#8221;;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;createString&nbsp;=&nbsp;&#8220;CREATE&nbsp;TABLE&nbsp;Employee&nbsp;(NAME&nbsp;VARCHAR(32)&nbsp;NOT&nbsp;NULL,&nbsp;ADDRESS&nbsp;VARCHAR(50)&nbsp;NOT&nbsp;NULL)&#8221;;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class.forName(driver);&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(java.lang.ClassNotFoundException&nbsp;e)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn&nbsp;=&nbsp;DriverManager.getConnection(connectionURL);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement&nbsp;stmt&nbsp;=&nbsp;conn.createStatement();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stmt.executeUpdate(createString);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PreparedStatement&nbsp;psInsert&nbsp;=&nbsp;conn.prepareStatement(&#8220;insert&nbsp;into&nbsp;Employee&nbsp;values&nbsp;(?,?)&#8221;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;psInsert.setString(1,&nbsp;args[0]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;psInsert.setString(2,&nbsp;args[1]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;psInsert.executeUpdate();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement&nbsp;stmt2&nbsp;=&nbsp;conn.createStatement();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ResultSet&nbsp;rs&nbsp;=&nbsp;stmt2.executeQuery(&#8220;select&nbsp;*&nbsp;from&nbsp;Employee&#8221;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;num&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(rs.next())&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(++num&nbsp;+&nbsp;&#8220;:&nbsp;Name:&nbsp;&#8221;&nbsp;+&nbsp;rs.getString(1)&nbsp;+&nbsp;&#8220;\\n&nbsp;Address&#8221;&nbsp;+&nbsp;rs.getString(2));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rs.close();&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}}<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,7],"tags":[139],"class_list":["post-428","post","type-post","status-publish","format-standard","hentry","category-java","category-nosql","tag-derby"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=428"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/428\/revisions"}],"predecessor-version":[{"id":947,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/428\/revisions\/947"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}