Skip to content
Snippets Groups Projects
Commit 19a43ba6 authored by Joel Cavat's avatar Joel Cavat
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
# Java 11 + MySQL + JDBC Driver
### How to run
```
mvn package
mvn exec:java
```
pom.xml 0 → 100644
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.hepia</groupId>
<artifactId>my-project</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- The plugin provides 2 goals to help execute system and Java programs. -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>ch.hepia.App</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
<!-- The Surefire Plugin is used during the test phase of the build lifecycle
to execute the unit tests of an application -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<!-- Used to create a jar with all dependencies (mvn package will create it) -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package ch.hepia;
//STEP 1. Import required packages
import java.sql.*;
public class App {
// JDBC driver name and database URL
static final String driver = "com.mysql.cj.jdbc.Driver";
static final String dbUrl = "jdbc:mysql://localhost:3306/conf?useSSL=false";
// Database credentials
static final String user = "root";
static final String pass = "test";
public static void main(String[] args) throws ClassNotFoundException {
Class.forName(driver);
try (Connection con = DriverManager.getConnection(dbUrl, user, pass);
PreparedStatement ps = con.prepareStatement("SELECT * FROM Conference");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
int id = rs.getInt("id_conference");
String name = rs.getString("name");
String price = rs.getString("price");
System.out.println(id + ": " + name + ", " + price);
}
System.out.println("Goodbye");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment