JDBC Simple Connection : Connection : Database SQL JDBC : Java examples (example source code) Organized by topic

Java
C++
PHP


Java  »  Database SQL JDBC   » [  Connection  ]  Screenshots 
 



JDBC Simple Connection

/*

Database Programming with JDBC and Java, Second Edition
By George Reese
ISBN: 1-56592-616-1

Publisher: O'Reilly

*/


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Example 3.2. The SimpleConnection class is a command line application that
 * accepts the following command line: java SimpleConnection DRIVER URL UID
 * PASSWORD If the URL fits the specified driver, it will then load the driver
 * and get a connection.
 */
public class SimpleConnection {
  static public void main(String args[]) {
    Connection connection = null;

    // Process the command line
    if (args.length != 4) {
      System.out.println("Syntax: java SimpleConnection "
          "DRIVER URL UID PASSWORD");
      return;
    }
    try // load the driver
      Class.forName(args[0]).newInstance();
    catch (Exception e) { // problem loading driver, class not exist?
      e.printStackTrace();
      return;
    }
    try {
      connection = DriverManager.getConnection(args[1], args[2], args[3]);
      System.out.println("Connection successful!");
      // Do whatever queries or updates you want here!!!
    catch (SQLException e) {
      e.printStackTrace();
    finally {
      if (connection != null) {
        try {
          connection.close();
        catch (SQLException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

           
       
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.  Load some drivers
7.  Encapsulate the Connection-related operations that every JDBC program seems to use
8.  Test of loading a driver and connecting to a database
9.  Load MySQL JDBC Driver
10.  Oracle JDBC Driver load
11.  Oracle JDBC Driver load test: NewInstance
12.  Test Register Oracle JDBC Driver
13.  Install Oracle Driver and Execute Resultset
14.  Test Thin Net8 App
























Home| Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.