This program tests that the database and the JDBC driver are correctly configured. : Connection « Database SQL JDBC « Java






This program tests that the database and the JDBC driver are correctly configured.

   
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.sql.*;
import java.io.*;
import java.util.*;

/**
 * This program tests that the database and the JDBC driver are correctly configured.
 * @version 1.01 2004-09-24
 * @author Cay Horstmann
 */
class TestDB
{
   public static void main(String args[])
   {
      try
      {
         runTest();
      }
      catch (SQLException ex)
      {
         for (Throwable t : ex)
            t.printStackTrace();
      }
      catch (IOException ex)
      {
         ex.printStackTrace();
      }
   }

   /**
    * Runs a test by creating a table, adding a value, showing the table contents, and removing the
    * table.
    */
   public static void runTest() throws SQLException, IOException
   {
      Connection conn = getConnection();
      try
      {
         Statement stat = conn.createStatement();

         stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))");
         stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");

         ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
         if (result.next())
            System.out.println(result.getString(1));
         result.close();
         stat.executeUpdate("DROP TABLE Greetings");
      }
      finally
      {
         conn.close();
      }
   }

   /**
    * Gets a connection from the properties specified in the file database.properties
    * @return the database connection
    */
   public static Connection getConnection() throws SQLException, IOException
   {
      Properties props = new Properties();
      FileInputStream in = new FileInputStream("database.properties");
      props.load(in);
      in.close();

      String drivers = props.getProperty("jdbc.drivers");
      if (drivers != null) System.setProperty("jdbc.drivers", drivers);
      String url = props.getProperty("jdbc.url");
      String username = props.getProperty("jdbc.username");
      String password = props.getProperty("jdbc.password");

      return DriverManager.getConnection(url, username, password);
   }
}


//File: database.properties
#jdbc.drivers=org.apache.derby.jdbc.ClientDriver
jdbc.url=jdbc:derby://localhost:1527/COREJAVA;create=true
jdbc.username=dbuser
jdbc.password=secret

   
    
    
  








Related examples in the same category

1.Connect to more than one database
2.Verify database setup
3.Debug Database connection
4.Create Connection With Properties
5.Set save point
6.JDBC Simple Connection
7.Load some drivers
8.Encapsulate the Connection-related operations that every JDBC program seems to use
9.Test of loading a driver and connecting to a database
10.Load MySQL JDBC Driver
11.Oracle JDBC Driver load
12.Oracle JDBC Driver load test: NewInstance
13.Test Register Oracle JDBC Driver
14.Install Oracle Driver and Execute Resultset
15.Test Thin Net8 App
16.Specify a CharSet when connecting to a DBMS
17.Listing All Available Parameters for Creating a JDBC Connection
18.String java.sql.DriverPropertyInfo.name (Get name of property)
19.boolean java.sql.DriverPropertyInfo.required (Is property value required?)
20.String java.sql.DriverPropertyInfo.value (Get current value)
21.String java.sql.DriverPropertyInfo.description (Get description of property)
22.String[] java.sql.DriverPropertyInfo.choices (Get possible choices for property; if null, value can be any string)
23.Determining If a Database Supports Transactions
24.Committing and Rolling Back Updates to a Database
25.Disable auto commit mode in JDBC
26.Print warnings on a Connection to STDERR.
27.Print warnings on a Connection to a specified PrintWriter.
28.Manage Db connections providing shortcuts for Statements, PreparedStatements and ResultSets.