Date Created: Fri 21-Jan-2011

Related Document Categories:


    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

Middleware Mentor - Steven Charles Robinson

About Me

Steve Robinson has been working in IT for over 15 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware consulting. Steve comes from both an administration and development background.

Before moving to JEE, Steve was an accomplished developer and consultant for both IBM Lotus Notes and Microsoft .NET Technologies.

Follow Steve as @stevencrobinson on twitter.

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

Oracle WebLogic Categories

JBoss Categories

Other Categories