{"id":865,"date":"2021-01-02T20:05:11","date_gmt":"2021-01-02T10:05:11","guid":{"rendered":"http:\/\/www.nickdu.com\/?p=865"},"modified":"2021-01-02T20:05:11","modified_gmt":"2021-01-02T10:05:11","slug":"connect-a-java-database-through-c-net","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=865","title":{"rendered":"Connect a Java database via C# .Net"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I am currently working on a software project that involves connecting to a Java database (<a rel=\"noreferrer noopener\" href=\"https:\/\/db.apache.org\/derby\/\" target=\"_blank\">Apache Derby<\/a>) via a .Net C# application. &nbsp;I googled a lot and nothing helps. Finally, I decided to find my own one, and managed to get it up running. Here is my solution to share:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tools we need:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Java database engine JDBC driver, here I use Apache Derby, you can download from <a rel=\"noreferrer noopener\" href=\"https:\/\/db.apache.org\/derby\/derby_downloads.html#Change+History\" target=\"_blank\">https:\/\/db.apache.org\/derby\/derby_downloads.html#Change+History<\/a><\/li><li>IKVM.NET, you can download from  <a rel=\"noreferrer noopener\" href=\"https:\/\/www.nuget.org\/packages\/IKVM\/\" target=\"_blank\">https:\/\/www.nuget.org\/packages\/IKVM\/<\/a><\/li><li>Convert JAR to DLL<ul><li><img decoding=\"async\" class=\"wp-image-871\" style=\"width: 150px;\" src=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2021\/01\/jar2dll.png\" alt=\"\"><\/li><\/ul><\/li><li>Create a .Net Console application and reference DLLs as well as IKVM<ul><li><img decoding=\"async\" class=\"wp-image-872\" style=\"width: 150px;\" src=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2021\/01\/reference.png\" alt=\"\"><\/li><\/ul><\/li><li>Test it out<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>using java.lang;\nusing java.sql;\nusing System;\nusing org.apache.derby.jdbc;\n\nnamespace DerbyDB\n{\n    class Program\n    {\n        static void Main(string&#91;] args)\n        {\n            try\n            {\n                Class.forName(\"org.apache.derby.jdbc.EmbeddedDriver\").newInstance();\n\n                DriverManager.registerDriver(new EmbeddedDriver());\n                var derbyDBConnection = DriverManager.getConnection(\"jdbc:derby:C:\/DerbyDB\");\n                if (derbyDBConnection != null)\n                {\n                    Statement st = derbyDBConnection.createStatement();\n                    ResultSet rs = st.executeQuery(\"select * from eventlog\");\n                    while (rs.next())\n                    {\n                        Console.WriteLine(rs.getInt(\"ID\"));\n                        Console.WriteLine(rs.getTime(\"EVENT_DATE\"));\n                        Console.WriteLine(rs.getString(\"EVENT_MESSAGE\"));\n                    }\n                    st.close();\n                    derbyDBConnection.close();\n                }\n            }\n            catch (java.lang.Exception e)\n            {\n                Console.WriteLine(e);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am currently working on a software project that involves connecting to a Java database (Apache Derby) via a .Net C# application. &nbsp;I googled a lot and nothing helps. Finally, I decided to find my own one, and managed to get it up running. Here is my solution to share: Tools we need: Java database &hellip; <a href=\"https:\/\/nickdu.com\/?p=865\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Connect a Java database via C# .Net&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-865","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=865"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/865\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}