{"id":36275,"date":"2015-02-03T22:32:49","date_gmt":"2015-02-03T20:32:49","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=36275"},"modified":"2023-12-05T16:30:09","modified_gmt":"2023-12-05T14:30:09","slug":"jdbc-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html","title":{"rendered":"JDBC Tutorial &#8211; The ULTIMATE Guide (PDF Download)"},"content":{"rendered":"<p>In this post, we feature a comprehensive JDBC&nbsp; Tutorial (Java Database Connectivity), an API provided by Oracle that allows programmers to handle different databases from Java applications: it allows developers to establish connections to databases, defines how a specific client can access a given database, provides mechanisms for reading, inserting, updating and deleting entries of data in a database and takes care of transactions composed of different SQL statements.<\/p>\n<p>In this article we will explain the main JDBC components like Statements, Result Sets or Stored Procedures.<br \/>\nJDBC needs drivers for the different databases that programmers may want to work with; we will explain this in detail and we will provide some examples.<\/p>\n<p>JDBC comes together with Java since the beginning of times; the first release came with the JDK 1.1 on February 1997 and since then, JDBC has been an important part of Java. The main packages where JDBC is contained are <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/sql\/package-summary.html\"> http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/sql\/package-summary.html<\/a> and <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sql\/package-summary.html\"> http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sql\/package-summary.html<\/a>.<\/p>\n<p>All the information about the last JDBC release (4.2) and its development and maintenance can be found in the <a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=221\">JSR 221<\/a>.<\/p>\n<p>All examples shown in this article have been implemented using Java 8 update 0_25 and the Eclipse SDK version Luna 4.4. At the end of the article you can download all these examples and some more!<\/p>\n<div class=\"toc\">\n<h2>Table of Contents<\/h2>\n<dl>\n<dt><a href=\"#components\">1. Components <\/a><\/dt>\n<dt><a href=\"#connections\">2. Connections <\/a><\/dt>\n<dt><a href=\"#dataTypes\">3. Data types <\/a><\/dt>\n<dt><a href=\"#drivers\">4. Drivers <\/a><\/dt>\n<dt><a href=\"#dbs\">5. Databases <\/a><\/dt>\n<dt><a href=\"#resultsets\">6. Result sets <\/a><\/dt>\n<dt><a href=\"#stored\">7. Stored procedures <\/a><\/dt>\n<dt><a href=\"#statements\">8. Statements<\/a><\/dt>\n<dt><a href=\"#batch\">9. Batch commands<\/a><\/dt>\n<dt><a href=\"#transactions\">10. Transactions <\/a><\/dt>\n<dt><a href=\"#crud\">11. CRUD commands <\/a><\/dt>\n<dt><a href=\"#java8\">12. Java 8<\/a><\/dt>\n<dt><a href=\"#libraries\">13. Sql libraries built upon JDBC <\/a><\/dt>\n<dt><a href=\"#unitTesting\">14. Unit Testing <\/a><\/dt>\n<dt><a href=\"#summary\">15. Summary <\/a><\/dt>\n<dt><a href=\"#download\">16. Download <\/a><\/dt>\n<dt><a href=\"#links\">17. Links <\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"components\"><\/a>1. Components<\/h2>\n<p>The JDBC API allows programmers and Java applications to interact with databases. It supports executing different SQL statements and handling results coming from different data sources.<\/p>\n<p>In this section we will try to summarize and list the most important JDBC components that are part of every Java application, all of them will be explained in more detail in the next chapters.<\/p>\n<ul>\n<li>First of all, Java applications need to create and establish a connection ao a specific database. This is done using a Driver Manager, for example, one instance of the interface <code><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/sql\/DriverManager.html\">java.sql.DriverManager<\/a><\/code>, or directly via a JDBC data source. For this purpose, the interface<code><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/sql\/DataSource.html\">javax.sql.DataSource<\/a><\/code> can be used. As already mentioned, we will explain these components in more in detail in the next chapters.<\/li>\n<li>Once we are connected against a database, we can use our <code><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/sql\/Connection.html\"> java.sql.Connection<\/a><\/code> for executing CRUD (create, read, update, delete) SQL statements or operations. These statements are explained afterwards in this tutorial.<\/li>\n<li>In order to execute these these operations, programmers can use <code><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/sql\/Statement.html\">java.sql.Statement<\/a><\/code> and <code><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/sql\/PreparedStatement.html\">java.sql.PreparedStatement<\/a><\/code> based classes. The last ones are more efficient when executing the same statement several times and provide other benefits that we will list in this tutorial.<br \/>\nThe interface JDBC connection provides mechanisms to create statement instances:<\/li>\n<\/ul>\n<pre class=\"brush:java\"> \nPreparedStatement countriesStatement = connection.prepareStatement(\"UPDATE COUNTRIES SET NAME = ? WHERE ID = ?\");\ncountriesStatement.setString(1, \"Spain\");\ncountriesStatement.setInt(2, 123456789);\n<\/pre>\n<ul>\n<li>Operations like Insert, update or delete return back the number of modified rows and nothing else:<\/li>\n<\/ul>\n<pre class=\"brush:java\"> \n\/\/ countriesStatement belongs to the class Statement, returning number of updated rows\nint n = countriesStatement.executeUpdate();\n<\/pre>\n<ul>\n<li>Selection operations (queries) return results as rows inside a <code><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/sql\/ResultSet.html\">java.sql.ResultSet<\/a><\/code>. Rows are retrieved by name or number; results metadata is also available:<\/li>\n<\/ul>\n<pre class=\"brush:java\"> \n\/\/ countriesStatement belongs to the class Statement\nResultSet rs = countriesStatement.executeQuery(\"SELECT NAME, POPULATION FROM COUNTRIES\");\n\/\/rs contains the results in rows plus some metadata\n...\n<\/pre>\n<ul>\n<li>Normally, JDBC uses connection pools for managing connections. There are different implementations for connection pools like C3P0 or DBCP. These are groups of JDBC connections that are used or borrowed from the applications when needed and released when the task is finished. There is a lot of documentation about how to use and configure connection pools within JDBC, a good tutorial can be found in the following link <a href=\"http:\/\/docs.oracle.com\/cd\/E13222_01\/wls\/docs81\/ConsoleHelp\/jdbc_connection_pools.html\"> http:\/\/docs.oracle.com\/cd\/E13222_01\/wls\/docs81\/ConsoleHelp\/jdbc_connection_pools.html <\/a>.<\/li>\n<li>Other features are available while working with JDBC: Stored Procedures, Callable Statements, Batch Processing&#8230;all these will be described in this tutorial.<\/li>\n<\/ul>\n<h2><a name=\"connections\"><\/a>2. Connections<\/h2>\n<p>In order to connect to a database we need to use a <code>java.sql.Connection<\/code> object. We can do this using the <code>getConnection()<\/code> method of the <code>java.sql.DriverManager<\/code> class. This methods receives the database host and credentials as parameters.<\/p>\n<p>This snippet shows how to create a connection for a local MySQL database.<\/p>\n<pre class=\"brush:java\">\/\/MySQL driver is loaded\nClass.forName( \"com.mysql.jdbc.Driver\" );\n\/\/Connection object is created using the db host and credentials\nConnection connect = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost\/countries?\"\n            + \"user=root&amp;password=root\" );\n<\/pre>\n<p>A connection objects allows programmers to do the following actions:<\/p>\n<ul>\n<li>Creation of JDBC Statements: Using a connection object is possible to create <code>Statement<\/code>, <code>PreparedStatement<\/code> or <code>CallableStatement<\/code> instances that offer methods to execute different SQL statements. Here is an example of the creation of a <code>PreparedStatement<\/code>:<\/li>\n<\/ul>\n<pre class=\"brush:java\">\/\/the connection conn is used to create a prepared statement with the given sql operation\nPreparedStatement updateStmt = conn.prepareStatement( sql );\n<\/pre>\n<p>This statement can execute the sql update passed as parameter.<\/p>\n<ul>\n<li>Offers the possibility to commit or rollback a given transaction. JDBC connection supports two different ways of working: <code>autocommit=true<\/code> and <code>autocommit=false<\/code>. The first one commits all transactions directly to the database, the second one needs an special command in order to commit or rollback the transactions. We will see this is more detail in the related chapter in this tutorial. The following piece of code shows how to change the auto commit mode of a JDBC connection :<\/li>\n<\/ul>\n<pre class=\"brush:java\">\/\/it changes the mode to auto commit=false\nconnect.setAutoCommit( false );\n<\/pre>\n<ul>\n<li>Possibility to get meta information about the database that is been used.<\/li>\n<li>Other options like batch processing, stored procedures, etc.<\/li>\n<\/ul>\n<p>We will explain all these features in detail, for the moment it is good to know what a JDBC Connection is and what can be done using JDBC Connections.<\/p>\n<h2><a name=\"dataTypes\"><\/a>3. Data types<\/h2>\n<p>JDBC converts the Java data types into proper JDBC types before using them in the database. There is a default mapping between Java and JDBC data types that provides consistency between database implementations and drivers.<\/p>\n<p>The following table contains these mappings:<\/p>\n<table border=\"1\" cellpadding=\"2\">\n<tbody>\n<tr>\n<th>SQL<\/th>\n<th>JDBC\/Java<\/th>\n<th>setter<\/th>\n<th>getter<\/th>\n<\/tr>\n<tr>\n<td>VARCHAR<\/td>\n<td>java.lang.String<\/td>\n<td>setString<\/td>\n<td>getString<\/td>\n<\/tr>\n<tr>\n<td>CHAR<\/td>\n<td>java.lang.String<\/td>\n<td>setString<\/td>\n<td>getString<\/td>\n<\/tr>\n<tr>\n<td>LONGVARCHAR<\/td>\n<td>java.lang.String<\/td>\n<td>setString<\/td>\n<td>getString<\/td>\n<\/tr>\n<tr>\n<td>BIT<\/td>\n<td>boolean<\/td>\n<td>setBoolean<\/td>\n<td>getBoolean<\/td>\n<\/tr>\n<tr>\n<td>NUMERIC<\/td>\n<td>BigDecimal<\/td>\n<td>setBigDecimal<\/td>\n<td>getBigDecimal<\/td>\n<\/tr>\n<tr>\n<td>TINYINT<\/td>\n<td>byte<\/td>\n<td>setByte<\/td>\n<td>getByte<\/td>\n<\/tr>\n<tr>\n<td>SMALLINT<\/td>\n<td>short<\/td>\n<td>setShort<\/td>\n<td>getShort<\/td>\n<\/tr>\n<tr>\n<td>INTEGER<\/td>\n<td>int<\/td>\n<td>setInt<\/td>\n<td>getInt<\/td>\n<\/tr>\n<tr>\n<td>BIGINT<\/td>\n<td>long<\/td>\n<td>setLong<\/td>\n<td>getLong<\/td>\n<\/tr>\n<tr>\n<td>REAL<\/td>\n<td>float<\/td>\n<td>setFloat<\/td>\n<td>getFloat<\/td>\n<\/tr>\n<tr>\n<td>FLOAT<\/td>\n<td>float<\/td>\n<td>setFloat<\/td>\n<td>getFloat<\/td>\n<\/tr>\n<tr>\n<td>DOUBLE<\/td>\n<td>double<\/td>\n<td>setDouble<\/td>\n<td>getDouble<\/td>\n<\/tr>\n<tr>\n<td>VARBINARY<\/td>\n<td>byte[ ]<\/td>\n<td>setBytes<\/td>\n<td>getBytes<\/td>\n<\/tr>\n<tr>\n<td>BINARY<\/td>\n<td>byte[ ]<\/td>\n<td>setBytes<\/td>\n<td>getBytes<\/td>\n<\/tr>\n<tr>\n<td>DATE<\/td>\n<td>java.sql.Date<\/td>\n<td>setDate<\/td>\n<td>getDate<\/td>\n<\/tr>\n<tr>\n<td>TIME<\/td>\n<td>java.sql.Time<\/td>\n<td>setTime<\/td>\n<td>getTime<\/td>\n<\/tr>\n<tr>\n<td>TIMESTAMP<\/td>\n<td>java.sql.Timestamp<\/td>\n<td>setTimestamp<\/td>\n<td>getTimestamp<\/td>\n<\/tr>\n<tr>\n<td>CLOB<\/td>\n<td>java.sql.Clob<\/td>\n<td>setClob<\/td>\n<td>getClob<\/td>\n<\/tr>\n<tr>\n<td>BLOB<\/td>\n<td>java.sql.Blob<\/td>\n<td>setBlob<\/td>\n<td>getBlob<\/td>\n<\/tr>\n<tr>\n<td>ARRAY<\/td>\n<td>java.sql.Array<\/td>\n<td>setARRAY<\/td>\n<td>getARRAY<\/td>\n<\/tr>\n<tr>\n<td>REF<\/td>\n<td>java.sql.Ref<\/td>\n<td>SetRef<\/td>\n<td>getRef<\/td>\n<\/tr>\n<tr>\n<td>STRUCT<\/td>\n<td>java.sql.Struct<\/td>\n<td>SetStruct<\/td>\n<td>getStruct<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Null values are treated differently in SQL and in Java. When handling with SQL null values in Java it is good to follow some best practices like avoiding the usage of primitive types, since they cannot be null but converted to their default values like 0 for int, false for booleans, etc.<\/p>\n<p>Instead of that, the usage of wrapper classes for the primitive types is recommended. The class <code>ResultSet<\/code> contains a method called <code>wasNull()<\/code> that is very useful in these scenarios. Here is an example of its usage:<\/p>\n<pre class=\"brush:java\">Statement stmt = conn.createStatement( );\nString sql = \"SELECT NAME, POPULATION FROM COUNTRIES\";\nResultSet rs = stmt.executeQuery(sql);\n\nint id = rs.getInt(1);\nif( rs.wasNull( ) ) {\n   id = 0;\n}\n<\/pre>\n<h2><a name=\"drivers\"><\/a>4. Drivers<\/h2>\n<p>The JDBC Driver Manager, <code>java.sql.DriverManager<\/code>, is one of the most important elements of the JDBC API. It is the basic service for handling a list of JDBC Drivers. It contains mechanisms and objects that allow Java applications to connect to a desired JDBC driver. It is in charge of managing the different types of JDBC database drivers. Summarizing the main task of the Driver Manager is to be aware of the list of available drivers and to handle the connection between the specific selected driver and the database.<\/p>\n<p>The most frequently used method of this class is <code>DriverManager.getConnetion()<\/code>. This method establishes a connection to a database.<\/p>\n<p>Here is an example of its use:<\/p>\n<pre class=\"brush:java\">\/\/ Create the connection with the default credentials\njava.sql.Connection conn = DriverManager.getConnection(\"jdbc:hsqldb:mem:mydb\", \"SA\", \"\" );\n<\/pre>\n<p>We can register drivers using the method <code> DriverManager.registerDriver().<\/code>:<\/p>\n<pre class=\"brush:java\">new org.hsqldb.jdbc.JDBCDriver();\nDriverManager.registerDriver( new org.hsqldb.jdbc.JDBCDriver() );\n<\/pre>\n<p>We can also load a driver by calling the <code>Class.forName()<\/code> method:<\/p>\n<pre class=\"brush:java\">\/\/ Loading the HSQLDB JDBC driver\nClass.forName( \"org.hsqldb.jdbc.JDBCDriver\" );\n\n...\n\n\/\/ connection to JDBC using mysql driver\nClass.forName( \"com.mysql.jdbc.Driver\" );\n<\/pre>\n<p>The main difference is that the method <code>registerDriver()<\/code> needs that the driver is available at compile time, loading the driver class does not require that the driver is available at compile time. After JDBC 4, there is no real need of calling these methods and applications do not need to register drivers individually neither to load the driver classes. It is also not recommended to register drivers manually using the method <code>registerDriver()<\/code>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Other interesting methods of the <code>DriverManager<\/code> class are <code> getDriver(String url)<\/code>, that tries to locate the driver by a given string and <code> getDrivers()<\/code> that returns an enumeration of all the drivers that has been previously registered in the Driver Manager:<\/p>\n<pre class=\"brush:java\">Enumeration drivers = DriverManager.getDrivers();\nwhile( drivers.hasMoreElements() )\n{\n\tDriver driver = drivers.nextElement();\n\tSystem.out.println( driver.getClass() );\n}\n<\/pre>\n<h2><a name=\"dbs\"><\/a>5. Databases<\/h2>\n<p>JDBC supports a large list of databases. It abstracts its differences and ways of working by using different Drivers. The <code>DriverManager<\/code> class is in charge of loading the proper database, after this is loaded, the code that access the database for querying and modifying data will remain (more or less) unchanged.<\/p>\n<p>Here is a list of supported databases in JDBC (officially registered within Oracle): <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/index-136695.html\"> http:\/\/www.oracle.com\/technetwork\/java\/index-136695.html<\/a>.<\/p>\n<p>In this chapter we are going to show how to use to different databases: MySQL and HSQLDB. The first one is very well known by programmers and wide used, the second one, HSQLDB, is a database very helpful for testing purposes that offers in memory capabilities. We will see how to use both and we will discover that except of the loading of the proper JDBC driver, the rest of the application remains unchanged:<\/p>\n<p>MySQL example:<\/p>\n<pre class=\"brush:java\">public static void main( String[] args ) throws ClassNotFoundException, SQLException\n    {\n\n        \/\/ connection to JDBC using mysql driver\n        Class.forName( \"com.mysql.jdbc.Driver\" );\n        Connection connect = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost\/countries?\"\n            + \"user=root&amp;password=root\" );\n\n       \n        selectAll( connect );\n\n        \/\/ close resources, in case of exception resources are not properly cleared\n...\n\n    }\n    \n    \/**\n     * select statement and print out results in a JDBC result set\n     * \n     * @param conn\n     * @throws SQLException\n     *\/\n    private static void selectAll( java.sql.Connection conn ) throws SQLException\n    {\n        Statement statement = conn.createStatement();\n\n        ResultSet resultSet = statement.executeQuery( \"select * from COUNTRIES\" );\n\n        while( resultSet.next() )\n        {\n            String name = resultSet.getString( \"NAME\" );\n            String population = resultSet.getString( \"POPULATION\" );\n\n            System.out.println( \"NAME: \" + name );\n            System.out.println( \"POPULATION: \" + population );\n        }\n\n    }\n\n<\/pre>\n<p>In memory (HSQLDB) example:<\/p>\n<pre class=\"brush:java\">public static void main( String[] args ) throws ClassNotFoundException, SQLException\n    {\n\n        \/\/ Loading the HSQLDB JDBC driver\n        Class.forName( \"org.hsqldb.jdbc.JDBCDriver\" );\n\n        \/\/ Create the connection with the default credentials\n        java.sql.Connection conn = DriverManager.getConnection( \"jdbc:hsqldb:mem:mydb\", \"SA\", \"\" );\n\n        \/\/ Create a table in memory\n        String countriesTableSQL = \"create memory table COUNTRIES (NAME varchar(256) not null primary key, POPULATION varchar(256) not null);\";\n\n        \/\/ execute the statement using JDBC normal Statements\n        Statement st = conn.createStatement();\n        st.execute( countriesTableSQL );\n\n        \/\/ nothing is in the database because it is just in memory, non persistent\n        selectAll( conn );\n\n        \/\/ after some insertions, the select shows something different, in the next execution these\n        \/\/ entries will not be there\n        insertRows( conn );\n        selectAll( conn );\n\n    }\n\n...\n\n    \/**\n     * select statement and print out results in a JDBC result set\n     * \n     * @param conn\n     * @throws SQLException\n     *\/\n    private static void selectAll( java.sql.Connection conn ) throws SQLException\n    {\n        Statement statement = conn.createStatement();\n\n        ResultSet resultSet = statement.executeQuery( \"select * from COUNTRIES\" );\n\n        while( resultSet.next() )\n        {\n            String name = resultSet.getString( \"NAME\" );\n            String population = resultSet.getString( \"POPULATION\" );\n\n            System.out.println( \"NAME: \" + name );\n            System.out.println( \"POPULATION: \" + population );\n        }\n\n    }\n\n<\/pre>\n<p>As we can see in last programs, the code of the <code>selectAll<\/code> methods is completely the same, only the JDBC Driver loading and connection creation changes; you can imagine how powerful this is when working in different environments. The HSQLDB version of the code contains also the piece of code in charge of creating the in memory database and inserting some rows, but this is just for showing and clarity purposes and can be done differently.<\/p>\n<h2><a name=\"resultsets\"><\/a>6. Result sets<\/h2>\n<p>The class <code>java.sql.ResultSet<\/code> represents a result set of database table. It is created, normally; by executing an SQL query (select statement using Statement or PreparedStatement). It contains rows of data, where the data is stored. These data can be accessed by index (starting by 1) or by attribute name:<\/p>\n<pre class=\"brush:java\">        \/\/ creating the result set\n        ResultSet resultSet = statement.executeQuery( \"select * from COUNTRIES\" );\n\n        \/\/ iterating through the results rows\n\n        while( resultSet.next() )\n        {\n            \/\/ accessing column values by index or name\n            String name = resultSet.getString( \"NAME\" );\n            int population = resultSet.getInt( \"POPULATION\" );\n\n            System.out.println( \"NAME: \" + name );\n            System.out.println( \"POPULATION: \" + population );\n\n\n            \/\/ accessing column values by index or name\n            String name = resultSet.getString( 1 );\n            int population = resultSet.getInt( 2 );\n\n            System.out.println( \"NAME: \" + name );\n            System.out.println( \"POPULATION: \" + population );\n\n\n        }\n\n<\/pre>\n<p>As shown before, ResultSets contain getter methods for retrieving column values for different Java types. It also contains a cursor pointing to the current row of data. Initially, the cursor is pointing before the first row. The next method moves the cursor to the next row: <code>java.sql.ResultSet.next()<\/code>.<\/p>\n<p>It is possible to create ResultSets with default properties like a cursor that moves forward only and that is not updatable. If programmers would like to use other kind of properties he can specify so in the creation of the Statement that is going to produce the result sets by changing the arguments passed:<\/p>\n<pre class=\"brush:java\">\/**\n* indicating result sets properties that will be created from this statement: type,\n* concunrrency and holdability\n*\/\nStatement statement = conn.createStatement( ResultSet. TYPE_SCROLL_INSENSITIVE,\nResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT );\n<\/pre>\n<p>Using this kind of result sets it is possible to move the cursor in both directions and to update or insert new data into the database using the result set with this purpose.<\/p>\n<h2><a name=\"stored\"><\/a>7. Stored procedures<\/h2>\n<p>In this chapter we are going to explain what stored procedures are and how we can use them within JDBC. For the examples we are going to use MySQL based stored procedures.<\/p>\n<p>Stored procedures are sets of SQL statements as part of a logical unit of executiion and performing a defined task. They are very useful while encapsulating a group of operations to be executed on a database.<\/p>\n<p>First of all we are going to create a procedure in our MySQL database, following script will help us with this task:<\/p>\n<pre class=\"brush:bash\">delimiter \/\/\n \nCREATE PROCEDURE spanish (OUT population_out INT)\n BEGIN\n SELECT COUNT(*) INTO population_out FROM countries;\n END\/\/\n \n \n delimiter ;\n \n CALL simpleproc(@a);\n \n<\/pre>\n<p>Basically the script above creates a procedure called Spanish with one output attribute of the type int and without input parameters. The procedure returns the count of all countries in the database.<\/p>\n<p>Once we have created the procedure we can work with it from our Java applications.In order to call Stored Procedures we need to use special statements of the interface <code><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/sql\/CallableStatement.html\">java.sql.CallableStatement<\/a><\/code>, these statements allow programmers to execute stored procedures indicating the output attributes and input parameters to be used. In our simple example, only output attributes are configured. Here is an example:<\/p>\n<pre class=\"brush:java\">        CallableStatement callableStatement = null;\n\n        \/\/ the procedure should be created in the database\n        String spanishProcedure = \"{call spanish(?)}\";\n\n        \/\/ callable statement is used\n        callableStatement = connect.prepareCall( spanishProcedure );\n\n        \/\/ out parameters, also in parameters are possible, not in this case\n        callableStatement.registerOutParameter( 1, java.sql.Types.VARCHAR );\n\n        \/\/ execute using the callable statement method executeUpdate\n        callableStatement.executeUpdate();\n\n        \/\/ attributes are retrieved by index\n        String total = callableStatement.getString( 1 );\n\n        System.out.println( \"amount of spanish countries \" + total );\n\n<\/pre>\n<p>We can appreciate how to indicate where to store the output of the procedure and how to execute it using the method <code>java.sql.PreparedStatement.executeUpdate()<\/code>. Stored procedures are supported in most of the databases but their syntax and behavior may differ, that is why there may be differences in the Java applications handling stored procedures depending on the databases where the procedures are stored.<\/p>\n<h2><a name=\"statements\"><\/a>8. Statements<\/h2>\n<p>As already mentioned in this tutorial, JDBC uses the interface <code>java.sql.Statement<\/code> to execute different SQL queries and operations like insert, update or delete. This is the basic interface that contains all the basic methods like <code>java.sql.Statement.executeQuery(String)<\/code> or <code>java.sql.Statement.executeUpdate(String)<\/code>.<\/p>\n<p>Implementations of this interface are recommended when programmers do not need to execute same query multiple times or when queries and statements do not need to be parameterized. In general, we can say that this interface is suitable when executing DDL statements (Create, Alter, Drop). These statements are not executed multiple times normally and do not need to support different parameters.<\/p>\n<p>In case programmers need better efficiency when repeating SQL queries or parameterization they should use <code>java.sql.PreparedStatement<\/code>. This interface inherits the basic statement interface mentioned before and offers parameterization. Because of this functionalitiy, this interface is safer against SQL injection attacks. Here is a piece of code showing an example of this interface:<\/p>\n<pre class=\"brush:java\">System.out.println( \"Updating rows for \" + name + \"...\" );\n\nString sql = \"UPDATE COUNTRIES SET POPULATION=? WHERE NAME=?\";\n\nPreparedStatement updateStmt = conn.prepareStatement( sql );\n\n\/\/ Bind values into the parameters.\nupdateStmt.setInt( 1, 10000000 ); \/\/ population\nupdateStmt.setString( 2, name ); \/\/ name\n\n\/\/ update prepared statement using executeUpdate\nint numberRows = updateStmt.executeUpdate();\n\nSystem.out.println( numberRows + \" rows updated...\" );\n<\/pre>\n<p>Another benefit of using prepared statements is the possibility to handle non standard objects by using the <code>setObject()<\/code> method. Here is an example:<\/p>\n<pre class=\"brush:java\">        PreparedStatement updateStmt2 = conn.prepareStatement( sql );\n\n        \/\/ Bind values into the parameters using setObject, can be used for any kind and type of\n        \/\/ parameter.\n        updateStmt2.setObject( 1, 10000000 ); \/\/ population\n        updateStmt2.setObject( 2, name ); \/\/ name\n\n        \/\/ update prepared statement using executeUpdate\n        numberRows = updateStmt2.executeUpdate();\n\n        System.out.println( numberRows + \" rows updated...\" );\n        updateStmt2.close();\n\n<\/pre>\n<p>As mentioned in the chapter related to stored procedures, another interface is available for this purpose, it is called <code>java.sql.CallableStatement<\/code> and extends the PreparedStatement one.<br \/>\n[ulp id=&#8217;mLDszwXltlaciFhL&#8217;]<br \/>\n&nbsp;<\/p>\n<h2><a name=\"batch\"><\/a>9. Batch commands<\/h2>\n<p>JDBC offers the possibility to execute a list of SQL statements as a batch, that is, all in a row. Depending on what type of Statements the programmers are using the code may differ but the general idea is the same. In the next snippet is shown how to use batch processing with <code>java.sql.Statement<\/code>:<\/p>\n<pre class=\"brush:java\">Statement statement = null;\n\nstatement = connect.createStatement();\n\n\/\/ adding batchs to the statement\nstatement.addBatch( \"update COUNTRIES set POPULATION=9000000 where NAME='USA'\" );\nstatement.addBatch( \"update COUNTRIES set POPULATION=9000000 where NAME='GERMANY'\" );\nstatement.addBatch( \"update COUNTRIES set POPULATION=9000000 where NAME='ARGENTINA'\" );\n\n\/\/ usage of the executeBatch method\nint[] recordsUpdated = statement.executeBatch();\n\nint total = 0;\nfor( int recordUpdated : recordsUpdated )\n{\n\ttotal += recordUpdated;\n}\n\nSystem.out.println( \"total records updated by batch \" + total );\n\n<\/pre>\n<p>And using <code>java.sql.PreparedStatement<\/code>:<\/p>\n<pre class=\"brush:java\">       String sql = \"update COUNTRIES set POPULATION=? where NAME=?\";\n\n        PreparedStatement preparedStatement = null;\n\n        preparedStatement = connect.prepareStatement( sql );\n\n        preparedStatement.setObject( 1, 1000000 );\n        preparedStatement.setObject( 2, \"SPAIN\" );\n\n        \/\/ adding batches\n        preparedStatement.addBatch();\n\n        preparedStatement.setObject( 1, 1000000 );\n        preparedStatement.setObject( 2, \"USA\" );\n\n        \/\/ adding batches\n        preparedStatement.addBatch();\n\n        \/\/ executing all batchs\n        int[] updatedRecords = preparedStatement.executeBatch();\n        int total = 0;\n        for( int recordUpdated : updatedRecords )\n        {\n            total += recordUpdated;\n        }\n\n        System.out.println( \"total records updated by batch \" + total );\n\n<\/pre>\n<p>We can see that the differences are basically the way the SQL query parameters are used and how the queries are built, but the idea of executing several statements on one row is the same. In the first case by using the method <code>java.sql.Statement.executeBatch()<\/code>, using <code>java.sql.PreparedStatement.addBatch()<\/code> and <code>java.sql.Statement.executeBatch()<\/code> in the second one.<\/p>\n<h2><a name=\"transactions\"><\/a>10. Transactions<\/h2>\n<p>JDBC supports transactions and contains methods and functionalities to implement transaction based applications. We are going to list the most important ones in this chapter.<\/p>\n<ul>\n<li><code>java.sql.Connection.setAutoCommit(boolean)<\/code>: This method receives a Boolean as parameter, in case of true (which is the default behavior), all SQL statements will be persisted automatically in the database. In case of false, changes will not be persisted automatically, this will be done by using the method <code>java.sql.Connection.commit()<\/code>.<\/li>\n<li><code>java.sql.Connection.commit()<\/code>. This method can be only used if the auto commit is set to false or disabled; that is, it only works on non automatic commit mode. When executing this method all changes since last commit \/ rollback will be persisted in the database.<\/li>\n<li><code>java.sql.Connection.rollback()<\/code>. This method can be used only when auto commit is disabled. It undoes or reverts all changes done in the current transaction.<\/li>\n<\/ul>\n<p>And here is an example of usage where we can see how to disable the auto commit mode by using the method <code>setAutoCommit(false)<\/code>. All changes are committed when calling <code>commit()<\/code> and current transaction changes are rolled back by using the method <code>rollback()<\/code>:<\/p>\n<pre class=\"brush:java\">Class.forName( \"com.mysql.jdbc.Driver\" );\nConnection connect = null;\ntry\n{\n    \/\/ connection to JDBC using mysql driver\n    connect = DriverManager.getConnection( \"jdbc:mysql:\/\/localhost\/countries?\"\n                + \"user=root&amp;password=root\" );\n    connect.setAutoCommit( false );\n\n    System.out.println( \"Inserting row for Japan...\" );\n    String sql = \"INSERT INTO COUNTRIES (NAME,POPULATION) VALUES ('JAPAN', '45000000')\";\n\n    PreparedStatement insertStmt = connect.prepareStatement( sql );\n\n    \/\/ insert statement using executeUpdate\n    insertStmt.executeUpdate( sql );\n    connect.rollback();\n\n    System.out.println( \"Updating row for Japan...\" );\n    \/\/ update statement using executeUpdate -&gt; will cause an error, update will not be\n    \/\/ executed becaues the row does not exist\n    sql = \"UPDATE COUNTRIES SET POPULATION='1000000' WHERE NAME='JAPAN'\";\n    PreparedStatement updateStmt = connect.prepareStatement( sql );\n\n    updateStmt.executeUpdate( sql );\n    connect.commit();\n\n}\ncatch( SQLException ex )\n{\n    ex.printStackTrace();\n    \/\/undoes all changes in current transaction\n    connect.rollback();\n}\nfinally\n{\n    connect.close();\n}\n<\/pre>\n<h2><a name=\"crud\"><\/a>11. CRUD commands<\/h2>\n<p>CRUD comes from Create, Read, Update and Delete. JDBC supports all these operations and commands, in this chapter we are going to show difference snippets of Java code performing all of them:<\/p>\n<p>Create Statement. It is possible to create databases using JDBC, here is an example of creation of a in memory database:<\/p>\n<pre class=\"brush:java\">\/\/ Create a table in memory\nString countriesTableSQL = \"create memory table COUNTRIES (NAME varchar(256) not null primary key, POPULATION varchar(256) not null);\";\n\n\/\/ execute the statement using JDBC normal Statements\nStatement st = conn.createStatement();\nst.execute( countriesTableSQL );\n<\/pre>\n<p>Insert Statement. Inserts are supported in JDBC. Programmers can use normal SQL syntax and pass them to the different statement classes that JDBC offers like <code>Statement<\/code>, <code>PreparedStatement<\/code> or <code>CallableStatement<\/code>. Here are a couple of examples:<\/p>\n<pre class=\"brush:java\">Statement insertStmt = conn.createStatement();\n\nString sql = \"INSERT INTO COUNTRIES (NAME,POPULATION) VALUES ('SPAIN', '45Mill')\";\ninsertStmt.executeUpdate( sql );\n\nsql = \"INSERT INTO COUNTRIES (NAME,POPULATION) VALUES ('USA', '200Mill')\";\ninsertStmt.executeUpdate( sql );\n\nsql = \"INSERT INTO COUNTRIES (NAME,POPULATION) VALUES ('GERMANY', '90Mill')\";\ninsertStmt.executeUpdate( sql );\n<\/pre>\n<p>These statements return the number of inserted rows. The same is applicable to update statements, here is an example of how to update a set of rows in a database:<\/p>\n<pre class=\"brush:java\">System.out.println( \"Updating rows for \" + name + \"...\" );\n\n Statement updateStmt = conn.createStatement();\n\n \/\/ update statement using executeUpdate\n String sql = \"UPDATE COUNTRIES SET POPULATION='10000000' WHERE NAME='\" + name + \"'\";\n int numberRows = updateStmt.executeUpdate( sql );\n\n System.out.println( numberRows + \" rows updated...\" );\n<\/pre>\n<p>The output would be:<\/p>\n<pre class=\"brush:bash\">Updating rows for SPAIN...\n4 rows updated...\n<\/pre>\n<p>Select Statement. It is possible to execute any (almost) kind of SQL query using JDBC statements. Here is a very simple example that reads all the rows of a given table and prints them out in the standard console:<\/p>\n<pre class=\"brush:java\">Statement statement = conn.createStatement();\n\nResultSet resultSet = statement.executeQuery( \"select * from COUNTRIES\" );\n\nwhile( resultSet.next() )\n{\n\tString name = resultSet.getString( \"NAME\" );\n      \tString population = resultSet.getString( \"POPULATION\" );\n\tSystem.out.println( \"NAME: \" + name );\n      \tSystem.out.println( \"POPULATION: \" + population );\n}\n\n<\/pre>\n<p>The output of this would be (depending on the database state):<\/p>\n<pre class=\"brush:bash\">NAME: GERMANY\nPOPULATION: 90Mill\nNAME: SPAIN\nPOPULATION: 45Mill\nNAME: USA\nPOPULATION: 200Mill\n<\/pre>\n<p>Delete statement. Finally, JDBC supports deletion of rows and dropping of tables and other SQL elements. Here is a snippet showing the deletion of all rows with an specific criteria (in this case, the name has to be &#8220;JAPAN&#8221;):<\/p>\n<pre class=\"brush:java\">System.out.println( \"Deleting rows for JAPAN...\" );\nString sql = \"DELETE FROM COUNTRIES WHERE NAME='JAPAN'\";\nPreparedStatement deleteStmt = connect.prepareStatement( sql );\n\n\/\/ delete statement using executeUpdate\nint numberRows = deleteStmt.executeUpdate( sql );\n\nSystem.out.println( numberRows + \" rows deleted...\" );\n<\/pre>\n<p>Delete statements return the number of affected rows, in this case the output would be (depending on the database state):<\/p>\n<pre class=\"brush:bash\">Deleting rows for JAPAN...\n0 rows deleted...\n<\/pre>\n<p>These examples are all very simple ones; they have been written for learning purposes but you can imagine that you can execute more complicated SQL queries just by changing the argument passed to the <code>executeQuery()<\/code> or <code>executeUpdate()<\/code> methods.<\/p>\n<h2><a name=\"java8\"><\/a>12. Java 8<\/h2>\n<p>Java 8 does not contain any major change related to JDBC or the JDBC framework. But several features of Java 8 can be applied when working with JDBC with very good results. We are going to show some of them. For example it is possible to execute select queries in a very different way as we are used to. Here is an example of how we do it without Java 8 features, it is more or less the same as we did in all our examples during this article:<\/p>\n<pre class=\"brush:java\">\/\/ we always need to write this code\nSystem.out.println( \"using Java 7\" );\n\/\/ connection to JDBC using mysql driver\nClass.forName( \"com.mysql.jdbc.Driver\" );\nConnection connect = DriverManager.getConnection( \"jdbc:mysql:\/\/localhost\/countries?\"\n            + \"user=root&amp;password=root\" );\n\n\/\/ select query\nPreparedStatement statement = connect.prepareStatement( \"select * from COUNTRIES\" );\nResultSet resultSet = statement.executeQuery();\n\n\/\/ iterating results\nwhile( resultSet.next() )\n{\n\t\/\/ access via name\n       Object name = resultSet.getObject( 1 );\n       Object population = resultSet.getObject( 2 );\n\n       System.out.println( \"Name: \" + name );\n       System.out.println( \"Population: \" + population );\n}\n\n\/\/ close resources, in case of exception resources are not properly cleared\nresultSet.close();\nstatement.close();\nconnect.close();\n<\/pre>\n<p>And here is a version that does the same but using Lambdas.<\/p>\n<pre class=\"brush:java\">\/\/ select method is called and lambda expression is provided, this expression will be used\n\/\/ in the handle method of the functional interface\nselect( connect, \"select * from COUNTRIES\", ( resultSet ) -&gt; {\n\tSystem.out.println( resultSet.getObject( 1 ) );\n       System.out.println( resultSet.getObject( 2 ) );\n} );\n<\/pre>\n<p>The piece of code shown above contains a select method call where the first parameter is the Connection object, the second parameter is an SQL query and the third one is a Lambda expression. This Lambda expression receives one parameter (instance of <code>ResultSet<\/code>) and prints out its first two attributes, but anything can be done with this result set in the body of the Lambda expression. Here is the implementation of the <code>select()<\/code> method:<\/p>\n<pre class=\"brush:java\">public static void select( Connection connect, String sql, ResultSetHandler handler ) throws SQLException\n    {\n        PreparedStatement statement = connect.prepareStatement( sql );\n\n        try (ResultSet rs = statement.executeQuery())\n        {\n            while( rs.next() )\n            {\n                handler.handle( rs );\n            }\n\n        }\n    }\n<\/pre>\n<p>And the functional interface <code>ResultSetHandler<\/code>:<\/p>\n<pre class=\"brush:java\">@FunctionalInterface\npublic interface ResultSetHandler\n{\n\n    \/**\n     * This method will be executed by the lambda expression\n     * \n     * @param resultSet\n     * @throws SQLException\n     *\/\n    public void handle( ResultSet resultSet ) throws SQLException;\n\n}\n<\/pre>\n<p>We can see here that the code is clearer and reduced drastically (or not) when using some of the new Java 8 features.<\/p>\n<h2><a name=\"libraries\"><\/a>13. Sql libraries built upon JDBC<\/h2>\n<p>JDBC is used by several well known Java libraries to build their APIs. In this section we are going to list some of them:<\/p>\n<ul>\n<li>HSQLDB (Hyper SQL Database) is a relational database management system that offers in memory and persistent storage. It has a JDBC driver (as shown in some of the examples). It is very useful for testing purposes because of its non persistent features and supports almost all the SQL core features. For more information please visit <a href=\"http:\/\/hsqldb.org\/\"> http:\/\/hsqldb.org\/<\/a><\/li>\n<li>DBUnit is an extension of JUnit. It is very useful for unit testing when databases are involved. This framework takes care of the databases state between tests and abstract several databases properties when testing. For downloading the sources and more documentation please visit <a href=\"http:\/\/www.dbunit.org\/\"> http:\/\/www.dbunit.org <\/a><\/li>\n<li>DBUtils is an Apache Commons library implemented with the goal of make the usage of JDBC much easier. Some of the features that this library contains are: clean up of resources, reduction of code quantity, easier and automatic population of result sets. This library is small, transparent and fast and should be used by developers who want to work directly with JDBC. Java 1.6 or higher is needed for using this library. For more documentation <a href=\"http:\/\/commons.apache.org\/proper\/commons-dbutils\/\"> http:\/\/commons.apache.org\/proper\/commons-dbutils\/<\/a><\/li>\n<li>Spring Data also contains a module related to JDBC. It is called Spring Data JDBC Extensions. It offers support for the most used features of JDBC. It offers special features for working with Oracle databases. If you want to learn more about this library please visit <a href=\"http:\/\/projects.spring.io\/spring-data-jdbc-ext\/\"> http:\/\/projects.spring.io\/spring-data-jdbc-ext\/ <\/a><\/li>\n<li>JOOQ is a very interesting framework from the company datageekery that uses JDBC; it generates Java code from SQL databases and offers an API to establish JDBC connections, querying data and handle the results in an easy way. For more information please visit their git hub account: <a href=\"https:\/\/github.com\/jOOQ\/jOOL\"> https:\/\/github.com\/jOOQ\/jOOL <\/a>.<\/li>\n<\/ul>\n<h2><a name=\"unitTesting\"><\/a>14. Unit Testing<\/h2>\n<p>When it comes to unit testing and databases there are always several questions open:<\/p>\n<ul>\n<li>What environment do we use for testing?<\/li>\n<li>Do we test with real data?<\/li>\n<li>Or do we use synthetic generated data?<\/li>\n<li>How do we test our databases without the proper credentials?<\/li>\n<\/ul>\n<p>Several libraries can help us with these tasks. In this chapter we are going to list some of them and provide some useful links where more information can be found:<\/p>\n<ul>\n<li>DBUnit: as stated before, DBUnit is a testing framework that works in collaboration with Junit. For more information <a href=\"http:\/\/dbunit.org\">http:\/\/dbunit.org<\/a><\/li>\n<li>TestNG: This testing framework covers a lot of testing scenarios like unit testing, functional testing, integration testing, etc. It is based on annotations. For more information about this framework please visit their web site: <a href=\"http:\/\/testng.org\/doc\/index.html\">http:\/\/testng.org\/doc\/index.html<\/a><\/li>\n<li>JOOQ. This framewok provides JDBC mocking and testing capabilities. It is very well documented and easy to use. For more information please visit <a href=\"http:\/\/jooq.org\">http:\/\/jooq.org<\/a><\/li>\n<\/ul>\n<h2><a name=\"summary\"><\/a>15. Summary<\/h2>\n<p>JDBC (Java Database Connectivity) is the standard API for Database connectivity between Java and a huge number of databases and data sources (from SQL based databases to Excel spreadsheets). In this tutorial we tried to explain the JDBC architecture and how to use it; we listed the main components that JDBC uses and we listed some of the drivers for different wide used databases like MySql.<\/p>\n<p>The most important points to remember are:<\/p>\n<ul>\n<li>Drivers are components that enable a Java application to work with a database. JDBC requires drivers for every specific database. A list of available drivers for JDBC can be found at <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/index-136695.html\"> http:\/\/www.oracle.com\/technetwork\/java\/index-136695.html<\/a>.<\/li>\n<li>SQL statements are sent directly to the database servers every time. JDBC contains a mechanism called <code>PreparedStatement<\/code> with predetermined execution path that provides more efficiency and better use of the resources.<\/li>\n<li>Result Sets are the representation used for rows coming out from a query.<\/li>\n<li>Stored Procedures are sets of SQL statements grouped together, they can be called by name without the need to call each separately.<\/li>\n<li>Transactions are groups of SQL statements. A transaction ends when <code>commit()<\/code> or <code>rollback()<\/code> are called. This grouping allow different to work in parallel.<\/li>\n<li>CRUD commands are <code>create<\/code>, <code>read<\/code>, <code>update<\/code> and <code>delete<\/code> commands. JDBC provide mechanisms to execute these commands.<\/li>\n<\/ul>\n<p>The tutorial contains some information related to new possibilities coming out with Java 8 related to JDBC like JOOQ; we also mentioned some important libraries implemented using JDBC like Spring-Data or Apache DBUtils.<\/p>\n<h2><a name=\"download\"><\/a>16. Download&nbsp;JDBC Tutorial Source Code<\/h2>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this tutorial here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/01\/jdbc_ultimate_tutorial.zip\"><strong>jdbc_ultimate_tutorial<\/strong><\/a>.<\/div>\n<h2><a name=\"links\"><\/a>17. Links<\/h2>\n<p>Apart of all the links and resources pointed during this article, if you are interested in learning more about the JDBC API and its features and mechanisms, the best up to date information source that you can find are the ones in the official Oracle web site:<\/p>\n<ul>\n<li><a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/sql\/package-summary.html\"> http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sql\/package-summary.html <\/a><\/li>\n<li><a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sql\/package-summary.html\"> http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sql\/package-summary.html <\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we feature a comprehensive JDBC&nbsp; Tutorial (Java Database Connectivity), an API provided by Oracle that allows programmers to handle different databases from Java applications: it allows developers to establish connections to databases, defines how a specific client can access a given database, provides mechanisms for reading, inserting, updating and deleting entries of &hellip;<\/p>\n","protected":false},"author":601,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[662,28,491,43,1039],"class_list":["post-36275","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-databases","tag-hsqldb","tag-jdbc","tag-mysql","tag-ultimate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JDBC Tutorial - The ULTIMATE Guide (PDF Download)<\/title>\n<meta name=\"description\" content=\"The Ultimate JDBC Tutorial is packed with examples on how to use the correct driver, how to obtain a connection, how to use a prepared statement and more!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JDBC Tutorial - The ULTIMATE Guide (PDF Download)\" \/>\n<meta property=\"og:description\" content=\"The Ultimate JDBC Tutorial is packed with examples on how to use the correct driver, how to obtain a connection, how to use a prepared statement and more!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-02-03T20:32:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-05T14:30:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Dani Buiza\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dani Buiza\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html\"},\"author\":{\"name\":\"Dani Buiza\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c1d18d0e5a1ad668bab93bb4124027e5\"},\"headline\":\"JDBC Tutorial &#8211; The ULTIMATE Guide (PDF Download)\",\"datePublished\":\"2015-02-03T20:32:49+00:00\",\"dateModified\":\"2023-12-05T14:30:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html\"},\"wordCount\":3712,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Databases\",\"HSQLDB\",\"JDBC\",\"MySQL\",\"Ultimate\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html\",\"name\":\"JDBC Tutorial - The ULTIMATE Guide (PDF Download)\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-02-03T20:32:49+00:00\",\"dateModified\":\"2023-12-05T14:30:09+00:00\",\"description\":\"The Ultimate JDBC Tutorial is packed with examples on how to use the correct driver, how to obtain a connection, how to use a prepared statement and more!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/02\\\/jdbc-tutorial.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"JDBC Tutorial &#8211; The ULTIMATE Guide (PDF Download)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c1d18d0e5a1ad668bab93bb4124027e5\",\"name\":\"Dani Buiza\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g\",\"caption\":\"Dani Buiza\"},\"description\":\"Daniel Gutierrez Diez holds a Master in Computer Science Engineering from the University of Oviedo (Spain) and a Post Grade as Specialist in Foreign Trade from the UNED (Spain). Daniel has been working for different clients and companies in several Java projects as programmer, designer, trainer, consultant and technical lead.\",\"sameAs\":[\"http:\\\/\\\/danibuiza.github.io\\\/yo\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/dani-buiza\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JDBC Tutorial - The ULTIMATE Guide (PDF Download)","description":"The Ultimate JDBC Tutorial is packed with examples on how to use the correct driver, how to obtain a connection, how to use a prepared statement and more!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"JDBC Tutorial - The ULTIMATE Guide (PDF Download)","og_description":"The Ultimate JDBC Tutorial is packed with examples on how to use the correct driver, how to obtain a connection, how to use a prepared statement and more!","og_url":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-02-03T20:32:49+00:00","article_modified_time":"2023-12-05T14:30:09+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Dani Buiza","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Dani Buiza","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html"},"author":{"name":"Dani Buiza","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c1d18d0e5a1ad668bab93bb4124027e5"},"headline":"JDBC Tutorial &#8211; The ULTIMATE Guide (PDF Download)","datePublished":"2015-02-03T20:32:49+00:00","dateModified":"2023-12-05T14:30:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html"},"wordCount":3712,"commentCount":7,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Databases","HSQLDB","JDBC","MySQL","Ultimate"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html","name":"JDBC Tutorial - The ULTIMATE Guide (PDF Download)","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-02-03T20:32:49+00:00","dateModified":"2023-12-05T14:30:09+00:00","description":"The Ultimate JDBC Tutorial is packed with examples on how to use the correct driver, how to obtain a connection, how to use a prepared statement and more!","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/02\/jdbc-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"JDBC Tutorial &#8211; The ULTIMATE Guide (PDF Download)"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c1d18d0e5a1ad668bab93bb4124027e5","name":"Dani Buiza","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g","caption":"Dani Buiza"},"description":"Daniel Gutierrez Diez holds a Master in Computer Science Engineering from the University of Oviedo (Spain) and a Post Grade as Specialist in Foreign Trade from the UNED (Spain). Daniel has been working for different clients and companies in several Java projects as programmer, designer, trainer, consultant and technical lead.","sameAs":["http:\/\/danibuiza.github.io\/yo\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/dani-buiza"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/601"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=36275"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36275\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=36275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=36275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=36275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}