import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TestDbConnection {

    public static String DRIVER = "com.mysql.jdbc.Driver";

    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://localhost:3306/";

        System.out.println(url);

        String userName = "root";
        String password = "";

        Class.forName(DRIVER);
        Connection conn = null;
        PreparedStatement pStmt = null;
        ResultSet rs = null;
        try {
            conn = DriverManager.getConnection(url, userName, password);
            String sql = "SELECT VERSION()";
            System.out.println(sql);

            pStmt = conn.prepareStatement(sql);
            rs = pStmt.executeQuery();
            while (rs.next()) {
                int COL_NUMBER = 1;
                System.out.println(rs.getObject(COL_NUMBER));
            }

            System.out.flush();
            Thread.sleep(100); // wait for System.out to finish flush
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (pStmt != null) {
                    pStmt.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

