Date Created: Fri 21-Jan-2011

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


    JDBC Test application to test a MySQL connection using Java
    Note: You will need to make sure that you have downloaded the mySQL JDBC driver and it is in the classpath.


    package com.test;
    import java.sql.*;

    public class JDBCTester {

    public static void main(String args[]) {
    System.out.println("JDBC Tester v1.0");
    try {
    Statement stmt;

    // Register the JDBC driver for MySQL.
    Class.forName("com.mysql.jdbc.Driver");

    // Define URL of database server for
    // database named mysql on the localhost
    // with the default port number 3306.
    String url = "jdbc:mysql://localhost:3306/mysql";

    // Get a connection to the database for a
    // user named xxx with a xxx password.
    Connection con = DriverManager.getConnection(url, "xxx","xxx");

    // Display URL and connection information
    System.out.println("URL: " + url);
    System.out.println("Connection: " + con);

    // Get a Statement object
    stmt = con.createStatement();

    //Execute a simple command;We do not trap the response
    //We are only wanting a connection test
    boolean result;
    result = stmt.execute("show tables;");
    if(result)
    {
    System.out.println("SQL command was executed sucessfully");
    }

    con.close();
    stmt.close();
    } catch (Exception e) {
    e.printStackTrace();
    }// end catch
    }// end main
    }// end class JDBCTester

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/

Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Other Categories