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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Database SQL JDBC » Connection 




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 != nullSystem.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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.