From: gu.martinm@gmail.com Date: Tue, 1 Jul 2014 19:06:42 +0000 (+0200) Subject: Working on JDBC examples X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=d8e9d6222b762f7603dabc31d8ee80fa3a3ab116;p=JavaForFun Working on JDBC examples --- diff --git a/Allgemeines/JDBC/src/main/java/de/sql/tests/RawJDBCExample.java b/Allgemeines/JDBC/src/main/java/de/sql/tests/RawJDBCExample.java index bed8b55..eb8b2f5 100644 --- a/Allgemeines/JDBC/src/main/java/de/sql/tests/RawJDBCExample.java +++ b/Allgemeines/JDBC/src/main/java/de/sql/tests/RawJDBCExample.java @@ -1,11 +1,13 @@ package de.sql.tests; +import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Types; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,55 +17,90 @@ public class RawJDBCExample { private static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/n2a?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"; - public static void main(final String[] args) { - Connection connection = null; - Statement statement = null; - ResultSet answer = null; - PreparedStatement preparedStatement = null; - try { - // 1. Register JDBC driver - // - // Register JDBC driver (by itself) with the DriverManager!!! - // (see static initializers in com.mysql.jdbc.Driver) - // - // otherwise, you could do it like this: - // DriverManager.registerDriver(new Driver()); (it should be the same) - // - Class.forName("com.mysql.jdbc.Driver"); - - // 2. Open a connection - logger.info("Connecting to database"); - connection = DriverManager.getConnection(RawJDBCExample.DB_URL, "root", ""); + public static void main(final String[] args) throws ClassNotFoundException, SQLException { + + // 1. Register JDBC driver + // + // Register JDBC driver (by itself) with the DriverManager!!! + // (see static initializers in com.mysql.jdbc.Driver) + // + // otherwise, you could do it like this: + // DriverManager.registerDriver(new Driver()); (it should be the same) + // + Class.forName("com.mysql.jdbc.Driver"); + + // 2. Open a connection + logger.info("Connecting to database"); + final Connection connection = DriverManager.getConnection(RawJDBCExample.DB_URL, "root", ""); + try { + // 3. Execute some query: statement logger.info("Execute statement"); - statement = connection.createStatement(); - answer = statement.executeQuery("SELECT * FROM AD"); - // Loop through ResultSet a row at a time - while (answer.next()) { - final int adID = answer.getInt("AD_ID"); - final int adCode = answer.getInt("AD_CODE"); - final String description = answer.getString("DESCRIPTION"); - logger.info("AD_ID: " + adID + " AD_CODE: " + adCode + " DESCRIPTION: " + description); - } + executeStatement(connection); logger.info("Statement executed successfully"); + // 4. Execute some query: prepared statement logger.info("Execute prepared statement"); - preparedStatement = connection.prepareStatement("SELECT * FROM AD"); - preparedStatement.executeQuery(); - // Loop through ResultSet a row at a time - while (answer.next()) { - final int adID = answer.getInt("AD_ID"); - final int adCode = answer.getInt("AD_CODE"); - final String description = answer.getString("DESCRIPTION"); - logger.info("AD_ID: " + adID + " AD_CODE: " + adCode + " DESCRIPTION: " + description); - } + preparedStatement(connection); logger.info("Prepared statement executed successfully"); - } catch (final ClassNotFoundException | SQLException e) { - logger.error("Program error: ", e); + + + // 5. Execute some query: callable statement + // JDBC also supports the execution of stored + // procedures and stored functions through a third type of statement object, + // the CallableStatement object created by the prepareCall() method. + logger.info("Execute callable statement"); + callableStatement(connection); + logger.info("Callable statement executed successfully"); + } finally { - if (answer != null) { + connection.close(); + } + } + + private static void executeStatement(final Connection connection) throws SQLException { + /** + * I MAY USE THE SAME Statement MORE THAN ONCE!!! :) + */ + + Statement statement = null; + ResultSet answer = null; + try { + statement = connection.createStatement(); + answer = statement.executeQuery("SELECT * FROM AD"); + // Loop through ResultSet a row at a time + while (answer.next()) { + final int adID = answer.getInt("AD_ID"); + final int adCode = answer.getInt("AD_CODE"); + final String description = answer.getString("DESCRIPTION"); + logger.info("AD_ID: " + adID + " AD_CODE: " + adCode + " DESCRIPTION: " + description); + } + } finally { + if (answer != null) { + // Explicitly close the cursor and connection. NOTE: IT IS NOT THE SAME AS "DECLARE CURSOR" OF SQL + // This is a cursor in program memory not in DBMS!!! + try { + answer.close(); // Cursor + } catch (final SQLException e) { + logger.error("Error while closing cursor: ", e); + } + } + } + + + try { + answer = statement.executeQuery("SELECT * FROM AD"); + // Loop through ResultSet a row at a time + while (answer.next()) { + final int adID = answer.getInt("AD_ID"); + final int adCode = answer.getInt("AD_CODE"); + final String description = answer.getString("DESCRIPTION"); + logger.info("AD_ID: " + adID + " AD_CODE: " + adCode + " DESCRIPTION: " + description); + } + } finally { + if (answer != null) { // Explicitly close the cursor and connection. NOTE: IT IS NOT THE SAME AS "DECLARE CURSOR" OF SQL // This is a cursor in program memory not in DBMS!!! try { @@ -73,28 +110,119 @@ public class RawJDBCExample { } } if (statement != null) { - // I think closing ResultSet should be enough... try { statement.close(); } catch (final SQLException e) { logger.error("Error while closing statement: ", e); } } - if (preparedStatement != null) { - // I think closing ResultSet should be enough... + } + } + + private static void preparedStatement(final Connection connection) throws SQLException { + /** + * I MAY USE THE SAME PreparedStatement MORE THAN ONCE!!! :) + */ + + PreparedStatement preparedStatement = null; + ResultSet answer = null; + try { + preparedStatement = connection.prepareStatement("SELECT * FROM AD"); + answer = preparedStatement.executeQuery(); + // Loop through ResultSet a row at a time + while (answer.next()) { + final int adID = answer.getInt("AD_ID"); + final int adCode = answer.getInt("AD_CODE"); + final String description = answer.getString("DESCRIPTION"); + logger.info("AD_ID: " + adID + " AD_CODE: " + adCode + " DESCRIPTION: " + description); + } + } finally { + if (answer != null) { + // Explicitly close the cursor and connection. NOTE: IT IS NOT THE SAME AS "DECLARE CURSOR" OF SQL + // This is a cursor in program memory not in DBMS!!! try { - preparedStatement.close(); + answer.close(); // Cursor } catch (final SQLException e) { - logger.error("Error while closing prepared statement: ", e); + logger.error("Error while closing cursor: ", e); } } - if (connection != null) { + } + + + try { + answer = preparedStatement.executeQuery(); + // Loop through ResultSet a row at a time + while (answer.next()) { + final int adID = answer.getInt("AD_ID"); + final int adCode = answer.getInt("AD_CODE"); + final String description = answer.getString("DESCRIPTION"); + logger.info("AD_ID: " + adID + " AD_CODE: " + adCode + " DESCRIPTION: " + description); + } + } finally { + if (answer != null) { + // Explicitly close the cursor and connection. NOTE: IT IS NOT THE SAME AS "DECLARE CURSOR" OF SQL + // This is a cursor in program memory not in DBMS!!! try { - connection.close(); + answer.close(); // Cursor } catch (final SQLException e) { - logger.error("Error while closing connection: ", e); + logger.error("Error while closing cursor: ", e); } } - } + if (preparedStatement != null) { + try { + preparedStatement.close(); + } catch (final SQLException e) { + logger.error("Error while closing statement: ", e); + } + } + } + } + + private static void callableStatement(final Connection connection) throws SQLException { + /** + * CREATE PROCEDURE CHANGE_REGION + * (IN OFFICE INTEGER, + * OUT OLD_REG VARCHAR(10), + * IN NEW_REG VARCHAR(10)) + * + * CREATE FUNCTION GET_REGION + * (IN OFFICE INTEGER) + * RETURNS VARCHAR(10) + */ + + CallableStatement cstmt1 = null; + CallableStatement cstmt2 = null; + try { + // Stored procedure: + final String str1 = "{CALL CHANGE_REGION(?, ?, ?)}"; + // Stored function: + final String str2 = "{? = CALL GET_REGION(?)}"; + + // Prepare the two statements + cstmt1 = connection.prepareCall(str1); + cstmt2 = connection.prepareCall(str2); + + // Go ahead and execute the call to the stored procedure + cstmt1.executeUpdate(); + final String adCode = cstmt1.getString("AD_CODE"); + + cstmt2.setInt("AD_ID", 666); + cstmt2.setString("AD_DESCRIPTION", "My ad"); + // returns a varchar parameter + cstmt2.registerOutParameter("AD_DESCRIPTION", Types.VARCHAR); + // Go ahead and execute the call to the stored function + cstmt2.executeUpdate(); + } finally { + try { + cstmt1.close(); + } catch (final SQLException e) { + logger.error("Error while closing statement: ", e); + } + try { + cstmt2.close(); + } catch (final SQLException e) { + logger.error("Error while closing statement: ", e); + } + } } } diff --git a/Allgemeines/JDBC/src/main/java/de/sql/tests/RawSpringJDBCWithPoolExample.java b/Allgemeines/JDBC/src/main/java/de/sql/tests/RawSpringJDBCWithPoolExample.java index 6813e83..696da09 100644 --- a/Allgemeines/JDBC/src/main/java/de/sql/tests/RawSpringJDBCWithPoolExample.java +++ b/Allgemeines/JDBC/src/main/java/de/sql/tests/RawSpringJDBCWithPoolExample.java @@ -12,6 +12,7 @@ import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcCall; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations; import org.springframework.jdbc.core.simple.SimpleJdbcOperations; @@ -53,6 +54,9 @@ public class RawSpringJDBCWithPoolExample { final SimpleJdbcInsertOperations simpleJdbcInsert = new SimpleJdbcInsert(dataSource); simpleJdbcInsert.withTableName("ad"); simpleJdbcInsert.execute(parameters); + + // 7. Using Spring SimpleJdbcCall + final SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(dataSource); // Now we close the whole pool :) ((ComboPooledDataSource)dataSource).close();