{"id":2614,"date":"2020-04-21T00:57:04","date_gmt":"2020-04-20T19:27:04","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2614"},"modified":"2020-04-21T00:57:04","modified_gmt":"2020-04-20T19:27:04","slug":"database-setup-mysql-oraclexe","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/database-setup-mysql-oraclexe\/","title":{"rendered":"Database setup MySQL or OracleXE"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">JDBC (Java Database Connectivity) is a Java platform library that provides API to connect to a database and operate with it. Nowadays, there are a lot of frameworks that are built for easier work with databases. But all of them contain the JDBC under the hood.<\/span><\/p>\n<h2><b>MySQL<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">MySQL is a very popular free and open-source relational databases, relational database management system (RDBMS).<br \/>\n<\/span><span style=\"font-weight: 400;\">Nowadays, the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/back-end-testing\/\">MySQL <\/a>solutions have become the leading database choice for web-based applications with its proven performance, reliability and ease-of-use.<br \/>\n<\/span><span style=\"font-weight: 400;\">The creator and the first sponsor of MySQL was the Swedish company MySQL AB. After the Sun Microsystems bought MySQL, then Oracle Corporation.<br \/>\n<\/span><span style=\"font-weight: 400;\">Oracle drives MySQL innovation, delivering new capabilities to power next-generation web, cloud, mobile, and embedded applications.<\/span><\/p>\n<h2><b>Setup MySQL database<\/b><\/h2>\n<ol>\n<li>\n<h4><span style=\"font-weight: 400;\"> Installing the driver.<\/span><\/h4>\n<\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">MySQL Connector\/J is a tool that provides connectivity to MySQL for client applications developed in the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-java\/\">Java programming language<\/a>.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Connector\/J implements the Java Database Connectivity API (JDBC 4.2).<br \/>\n<\/span><span style=\"font-weight: 400;\">Currently, there are two MySQL Connector\/J versions available:<br \/>\n<\/span><span style=\"font-weight: 400;\">Connector\/J 8.0 for JRE 8 or higher &#8211; recommended version.<br \/>\n<\/span><span style=\"font-weight: 400;\">Connector\/J 5.1 for JRE 5 or higher.<\/span><\/p>\n<p>We will use Connector\/J 8.0 for the Java 8 platform. For Java 7 or earlier, use Connector\/J 5.1 instead.<br \/>\nThere are several ways to install the Connector\/J package: installing from a binary distribution, installing from source, installing using Maven.<br \/>\nThe easiest method for installation is the binary distribution. If you use the source distribution you can customize your installation. You can download both types of distributions from the Connector\/J Download page.<br \/>\nThe source code for Connector\/J is also available on GitHub at\u00a0https:\/\/github.com\/mysql\/mysql-connector-j.<\/p>\n<p><span style=\"font-weight: 400;\">If you use Maven, you can find Connector\/J is available in the central repository:<\/span><\/p>\n<pre>&lt;dependency&gt;\r\n &lt;groupId&gt;mysql&lt;\/groupId&gt;\r\n &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\r\n &lt;version&gt;x.y.z&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<ol start=\"2\">\n<li>\n<h4><span style=\"font-weight: 400;\"> Obtaining the Connection.<\/span><\/h4>\n<\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Let&#8217;s get connection from <\/span><span style=\"font-weight: 400;\">DriverManager<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><strong>import<\/strong> java.sql.Connection;\r\n\r\n<strong>import<\/strong> java.sql.DriverManager;\r\n\r\n<strong>import<\/strong> java.sql.SQLException;\r\n\r\n<strong>public<\/strong> <strong>class<\/strong> ConnectionExample {\r\n\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>private<\/strong> <strong>final<\/strong> <strong>static<\/strong> String <strong><em>DB_NAME<\/em><\/strong>= \"testdb\";\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>private<\/strong> <strong>final<\/strong> <strong>static<\/strong> String <strong><em>USERNAME<\/em><\/strong>= \"root\";\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>private<\/strong> <strong>final<\/strong> <strong>static<\/strong> String <strong><em>PASSWORD<\/em><\/strong>= \"root\";\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String[] args) {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ load the driver class\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Class.<em>forName<\/em>(\"com.mysql.jdbc.Driver\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Connection <u>connection<\/u> = <strong>null<\/strong>;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>try<\/strong> {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 connection =\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0\u00a0 DriverManager.<em>getConnection<\/em>(\"jdbc:mysql:\/\/localhost\/\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 + <strong><em>DB_NAME<\/em><\/strong> + \"?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n                             serverTimezone=UTC&amp;useSSL=false\", <strong><em>USERNAME<\/em><\/strong>, <strong><em>PASSWORD<\/em><\/strong>);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"We got the connection!\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \/\/ work with connection\r\n\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } <strong>catch<\/strong> (SQLException ex) {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 System.<strong><em>out<\/em><\/strong>.println(\"SQLException: \" + ex.getMessage());\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>As you can see from imports, we are using classes from the package java.sql.<br \/>\nThe DriverManager method getConnection attempts to establish a connection to the given database URL. It tries to select an appropriate driver from the set of registered JDBC drivers. The basic form of URL is jdbc:subprotocol:subname. Our connection URL is:<\/p>\n<p>jdbc:mysql:\/\/localhost\/mysql?serverTimezone=UTC&amp;useSSL=false, where<\/p>\n<p>\u201cjdbc\u201d points to the API,<br \/>\n\u201cmysql\u201d is the database,<br \/>\n\u201clocalhost\u201d is the mysql server name (it may be also an IP address),<br \/>\n\u201c3306\u201d is the default port number(we haven&#8217;t changed it),<br \/>\n\u201ctestdb\u201d is the name of our database name (it can be any name, for example, \u201cmydatabase\u201d).<br \/>\nWe also used two configuration properties:<br \/>\n\u201cserverTimezone=UTC\u201d we set the default time zone. Without this property, we will get the warning that our time zone is unrecognized.<br \/>\n\u201c useSSL=false\u201d we disabled SSL. Without this property, we will get the warning that establishing an SSL connection without the server identity verification is not recommended. Another way to fix this warning is to provide a trust store for server certificate verification.<br \/>\nAlso, you have to send a username and password. The default username for the MySQL database is root. The password is given by the user during the installation of the MySQL database. We are going to use root as the password.<br \/>\nAfter getting the connection to the database, you can use it to create Statement and PreparedStatement objects, or retrieve metadata about the database.<\/p>\n<h3><strong>Basic database operations<\/strong><\/h3>\n<p>There are query and update statements.<br \/>\nThe <a href=\"https:\/\/www.h2kinfosys.com\/blog\/control-statements-in-java\/\">update statements<\/a> return an update count that indicates how many rows were affected in the database. They are as INSERT, UPDATE and DELETE.<br \/>\nThe query statements return a row result set(ResultSet class) and we can use it walk over the result set. We can retrieve an individual column in a row by name or by column number. The ResultSet class has metadata with the names of the columns and their types.<br \/>\nLet&#8217;s create a simple database table to demonstrate simple operations on the database with JDBC. You can use the MySQL Workbench for this purpose. We have created table Persons with columns ID, LastName, FirstName, Address, City. After, we added one row to this table. Look at the screenshot below:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-2618 \" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_174-1.png\" alt=\"\" width=\"729\" height=\"527\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_174-1.png 809w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_174-1-300x217.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_174-1-768x554.png 768w\" sizes=\"(max-width: 729px) 100vw, 729px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Here is our simple code to get the first row from our table and print the last name, first name, address and city of the person:<\/span><\/p>\n<pre><strong>import<\/strong> java.sql.Connection;\r\n<strong>import<\/strong> java.sql.DriverManager;\r\n<strong>import<\/strong> java.sql.ResultSet;\r\n<strong>import<\/strong> java.sql.SQLException;\r\n<strong>import<\/strong> java.sql.Statement;\r\n\r\n<strong>public<\/strong> <strong>class<\/strong> ConnectionExample {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String[] args) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Connection connection = <strong>null<\/strong>;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>try<\/strong> {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 connection =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0\u00a0 DriverManager.<em>getConnection<\/em>(\"jdbc:mysql:\/\/localhost\/testdb\" +\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"?serverTimezone=UTC&amp;useSSL=false\", \"root\", \"root\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"We got the connection!\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } <strong>catch<\/strong> (SQLException ex) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 System.<strong><em>out<\/em><\/strong>.println(\"SQLException: \" + ex.getMessage());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Statement statement = <strong>null<\/strong>;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ResultSet resultSet = <strong>null<\/strong>;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>try<\/strong> {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 statement = connection.createStatement();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 resultSet = statement.executeQuery(\"SELECT * FROM persons\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 <strong>if<\/strong> (statement.execute(\"SELECT * FROM persons\")) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 System.<strong><em>out<\/em><\/strong>.println(\"The query was executed!\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 resultSet = statement.getResultSet();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 <strong>while<\/strong> (resultSet.next()) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(resultSet.getString(\"LastName\"));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(resultSet.getString(\"FirstName\"));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(resultSet.getString(\"Address\"));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(resultSet.getString(\"City\"));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>catch<\/strong> (SQLException ex){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 System.<strong><em>out<\/em><\/strong>.println(\"SQLException: \" + ex.getMessage());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>finally<\/strong> {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \/\/let's release resources in a finally{} block\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \/\/ in reverse-order of their creation\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \/\/ if they are no-longer needed\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 <strong>if<\/strong> (resultSet != <strong>null<\/strong>) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <strong>try<\/strong> {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 resultSet.close();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } <strong>catch<\/strong> (SQLException sqlEx) { } \/\/ ignore\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 resultSet = <strong>null<\/strong>;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 <strong>if<\/strong> (statement != <strong>null<\/strong>) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <strong>try<\/strong> {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 statement.close();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } <strong>catch<\/strong> (SQLException sqlEx) { } \/\/ ignore\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 statement = <strong>null<\/strong>;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"All resources are released!\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">In this example, we have used Statement and ResultSet objects. The Statement object used for executing a static SQL statement and returning the results it produces. The ResultSet object is a table of data representing a database result set. For more information, you can look at Java documentation.<\/span><\/p>\n<h3>Oracle XE<\/h3>\n<p>Oracle XE (eXpress Edition) is another free database edition, developed by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Oracle_Corporation\" rel=\"nofollow noopener\" target=\"_blank\">Oracle Corporation<\/a>. It offers almost all features of the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/software-architecture\/\">Oracle database<\/a>, but have some limitations for resources, such as the size of user data, database RAM, amount of CPU threads and pluggable databases.<\/p>\n<h3>Setup Oracle XE database<\/h3>\n<p>1. Installing the driver<br \/>\nYou can upload the driver from the Oracle site. The latest version is Oracle Database 19c (19.3) JDBC Driver &amp; UCP. The archive contains the latest 19.3 JDBC Thin driver (ojdbc10.jar), the Universal Connection Pool (ucp.jar), their Readme(s) and companion jars. If you use Maven, you can add the dependency:<\/p>\n<pre><span style=\"font-weight: 400;\">&lt;dependency&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;groupId&gt;com.oracle.jdbc&lt;\/groupId&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;artifactId&gt;ojdbc8&lt;\/artifactId&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;version&gt;19.3.0.0&lt;\/version&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;dependency&gt;<\/span><\/pre>\n<p>2. Obtaining the Connection.<br \/>\nThe way to connect to the Oracle XE and to work with it is very similar to the MySQL. The main difference is the driver and the connection URL.<br \/>\nThe driver for Oracle XE is oracle.jdbc.driver.OracleDriver.<br \/>\nLet&#8217;s look at the code example:<\/p>\n<pre><b>import<\/b><span style=\"font-weight: 400;\"> java.sql.Connection;<\/span>\r\n<b>import<\/b><span style=\"font-weight: 400;\"> java.sql.DriverManager;<\/span>\r\n<b>import<\/b><span style=\"font-weight: 400;\"> java.sql.ResultSet;<\/span>\r\n<b>import<\/b><span style=\"font-weight: 400;\"> java.sql.Statement;<\/span>\r\n\r\n<b>public<\/b> <b>class<\/b><span style=\"font-weight: 400;\"> ConnectionExample {<\/span>\r\n<b>           private<\/b> <b>final<\/b> <b>static<\/b><span style=\"font-weight: 400;\"> String <\/span><b><i>user<\/i><\/b><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">\"system\"<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<b>           private<\/b> <b>final<\/b> <b>static<\/b><span style=\"font-weight: 400;\"> String <\/span><b><i>password<\/i><\/b><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">\"oracle\"<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n\r\n<b>            public<\/b> <b>static<\/b> <b>void<\/b><span style=\"font-weight: 400;\"> main(String <\/span><span style=\"font-weight: 400;\">args<\/span><span style=\"font-weight: 400;\">[]) {<\/span>\r\n<b>            try<\/b><span style=\"font-weight: 400;\"> {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">           \/\/ load the driver class<\/span>\r\n<span style=\"font-weight: 400;\">           Class.<\/span><i><span style=\"font-weight: 400;\">forName<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"oracle.jdbc.driver.OracleDriver\"<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0        <\/span> <span style=\"font-weight: 400;\">System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">\"The driver for Oracle database is loaded\"<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">           Connection <\/span><span style=\"font-weight: 400;\">connection<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">DriverManager.<\/span><i><span style=\"font-weight: 400;\">getConnection<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"jdbc:oracle:thin:@localhost:1521:xe\"<\/span><span style=\"font-weight: 400;\">,\u00a0<\/span>\r\n<b><i>                user<\/i><\/b><span style=\"font-weight: 400;\"> , <\/span><b><i>password<\/i><\/b><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0 <\/span>          <span style=\"font-weight: 400;\">System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">\"We are connected to Oracle database!\"<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">            \/\/create the statement object<\/span>\r\n<span style=\"font-weight: 400;\">             Statement <\/span><span style=\"font-weight: 400;\">stmt<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">connection<\/span><span style=\"font-weight: 400;\">.createStatement();<\/span>\r\n<span style=\"font-weight: 400;\">            \/\/execute query<\/span>\r\n\r\n<span style=\"font-weight: 400;\">                ResultSet <\/span><span style=\"font-weight: 400;\">rs<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">stmt<\/span><span style=\"font-weight: 400;\">.executeQuery(<\/span><span style=\"font-weight: 400;\">\"select * from persons\"<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">                   System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">\"The query was executed!\"<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<b>                while<\/b><span style=\"font-weight: 400;\"> (<\/span><span style=\"font-weight: 400;\">rs<\/span><span style=\"font-weight: 400;\">.next())<\/span>\r\n<span style=\"font-weight: 400;\">                  System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">rs<\/span><span style=\"font-weight: 400;\">.getInt(1));<\/span>\r\n\r\n<span style=\"font-weight: 400;\">                \/\/close the connection object<\/span>\r\n<span style=\"font-weight: 400;\">                   connection<\/span><span style=\"font-weight: 400;\">.close();<\/span>\r\n<span style=\"font-weight: 400;\">                     } <\/span><b>catch<\/b><span style=\"font-weight: 400;\"> (Exception <\/span><span style=\"font-weight: 400;\">e<\/span><span style=\"font-weight: 400;\">) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">                   System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">e<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">                   }<\/span>\r\n<span style=\"font-weight: 400;\">            }<\/span>\r\n<span style=\"font-weight: 400;\">        }<\/span><\/pre>\n<p>The connection URL for the oracle database is<br \/>\njdbc:oracle:thin:@localhost:1521:xe, where<br \/>\njdbc is the using API, oracle is the database name,<br \/>\nthin is the driver,<br \/>\nlocalhost is the server name with the running Oracle (we may also use IP address),<br \/>\n1521 is the port number,<br \/>\nXE is the Oracle service name.<br \/>\nYou may get this information from the tnsnames.ora file.<br \/>\nWe are using the default username for the oracle database (system).<br \/>\nThe password is given by the user during the installation of the Oracle database.<br \/>\nAfter getting connection, we can also create tables:<\/p>\n<pre><strong>import<\/strong> java.sql.Connection;\r\n<strong>import<\/strong> java.sql.DriverManager;\r\n<strong>import<\/strong> java.sql.Statement;\r\n\r\n<strong>public<\/strong> <strong>class<\/strong> ConnectionExample {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String args[]) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>try<\/strong> {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ load the driver class\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Class.<em>forName<\/em>(\"oracle.jdbc.driver.OracleDriver\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Connection connection = DriverManager.<em>getConnection<\/em>(\"jdbc:oracle:thin:@localhost:1521:xe\",\"system\",\"oracle\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"Creating new table!\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Statement stmt = connection.createStatement();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String sql = \"CREATE TEST_TABLE EMP ( ID NUMBER(5) PRIMARY KEY, NAME\u00a0VARCHAR2(50))\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 stmt.execute(sql);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"Table created successfully!\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 stmt.close();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } <strong>catch<\/strong> (Exception e) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(e);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">We can also use the PreparedStatement class. This statement is cached and has a pre-determined execution path on the database server. So, it&#8217;s allowing us to execute it multiple times in an efficient manner.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The JDBC also contains the CallableStatement class. This class is used for executing stored procedures on the database.<\/span><\/p>\n<p><b>Summary<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The JDBC is a database access protocol that allows Java developers to connect to a database and run SQL statements and queries on the database. The core <a href=\"https:\/\/www.h2kinfosys.com\/blog\/learn-oop-through-java\/\">Java class libraries<\/a> java.sql and javax.sql provide the JDBC APIs. The JDBC was created with the possibility to allow vendors to create drivers that offer the specialization for a particular database.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JDBC (Java Database Connectivity) is a Java platform library that provides API to connect to a database and operate with it. Nowadays, there are a lot of frameworks that are built for easier work with databases. But all of them contain the JDBC under the hood. MySQL MySQL is a very popular free and open-source [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[537,540,541,539,538],"class_list":["post-2614","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-database-setup-mysql","tag-java-database-connectivity","tag-jdbc","tag-mysql-database","tag-oraclexe"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2614"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2614\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2657"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}