JDBC - Hello World!

standalone, with Maven, harnessing Properties!

Directory Layout

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>jdbc-hello</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.jdbchello.HelloWorld</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

jdbc.properties
url = jdbc:mysql://sql7.freemysqlhosting.net:3306/sql7134711
username = sql7134711
password = Da!!

HelloWorld.java
package biz.tugay.jdbchello;
 
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 9/2/2016
 * Time: 5:16 PM
 */
public class HelloWorld {
 
    public static void main(String[] args) 
        throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.jdbc.Driver");
 
        String filename = "jdbc.properties";
        InputStream input = HelloWorld.class.getClassLoader().getResourceAsStream(filename);
 
        final Properties properties = new Properties();
        properties.load(input);
 
        final Connection connection = 
        DriverManager.getConnection(properties.getProperty("url"), 
                                    properties.getProperty("username"), 
                                    properties.getProperty("password"));
 
        final Statement statement = connection.createStatement();
        final ResultSet resultSet = statement.executeQuery("SELECT * FROM test");
 
        while (resultSet.next()) {
            final String string = resultSet.getString("DATA");
            System.out.println(string);
        }
    }
}

In Action