Commit d84a1d23 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Simple insert app, move Vaadin code to appendix

parent e616eb34
This diff is collapsed.
This diff is collapsed.
/A1.log
/.settings
/.idea
/*.iml
/.classpath
/.project
/target
<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>de.hdm_stuttgart.sda1.insert</groupId>
<artifactId>insert_user</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>insert_user</name>
<url>https://freedocs.mi.hdm-stuttgart.de/sd1SectToolsOfTheTrade2.html</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.8-dmr</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration />
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.hdm_stuttgart.sda1.insert.SimpleInsert</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package de.hdm_stuttgart.sda1.insert;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* <p>A JDBC "getting started" example inserting person's data into a table</p>
*
* <p>Caveat: No error handling whatsoever is being supplied!</p>
*
* <p>Preconditions:</p>
*
* <ul>
* <li>A database matching {@link #main(String[])}'s connection setup must be configured.</li>
* <li>A database table »Person« corresponding to this project's file »resources/schema.sql« must exist.</li>
* </ul>
*
*/
public class SimpleInsert {
/**
* <p>Disclaimer:</p>
*
* <ul>
* <li>User credentials (name and password) are being hard-coded requiring compile time
* database configuration. This is absolutely uuuuuuugly!</li>
* <li>Furthermore no exception handling whatsoever is being supplied.</li>
* </ul>
*
* <p>In other words: **Never** do this in any professional setting.</p>
*
* @param args unused
* @throws SQLException possible causes:
* <ul>
* <li>Driver matching JDBC URL unavailable.</li>
* <li>Missing network link to database server.</li>
* <li>Invalid credentials.</li>
* <li>Database table 'Person' does not exist.</li>
* <li>duplicate email (UNIQUE key).</li>
* <li>... something forgotten to think about?</li>
* </ul>
*/
public static void main(String[] args) throws SQLException {
// Step 1: Open a connection to the database server requiring
// DBC URL, username and password.
final Connection conn = DriverManager.getConnection (
"jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC&useSSL=true",
"hdmuser",
"XYZ");
// Step 2: Create a Statement instance
final Statement stmt = conn.createStatement();
// Step 3: Execute the desired INSERT
final int updateCount = stmt.executeUpdate(
"INSERT INTO Person VALUES('Jim', 'jim@foo.org')");
// Step 4: Provide feedback on execution. (However in case of
// an Exception we won't get here at all, sigh!)
System.out.println("Successfully inserted " + updateCount + " dataset(s)");
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<!-- You my want to define class or package level per-logger rules -->
<Logger name="de.hdm_stuttgart.sda1.insert.App" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
DROP TABLE IF EXISTS Person;
CREATE TABLE Person (
name char(80)
,email CHAR(20) UNIQUE
);
\ No newline at end of file
package de.hdm_stuttgart.sda1.insert;
/**
* No test defined yet, stay tuned!
*/
public class InsertTest {
}
\ No newline at end of file
......@@ -112,6 +112,8 @@
<module>Sda1/Streams/Solution</module>
<module>Sda1/Streams/Template</module>
<module>Sda1/Jdbc/Users/Plain</module>
<module>Sda1/Jdom/Catalog</module>
<module>Sda1/Jdom/Catalog2Xhtml</module>
<module>Sda1/Jdom/Elements</module>
......
package sda.jdbc.intro;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* A "getting started" example to insert data into a table by
* using JDBC. No error handling whatsoever is being supplied!
*/
public class SimpleInsert {
/**
* User credentials (name and password) are supplied within code. This is
* absolutely uuuuuuugly! For a Java property based solution
* see {@link sda.jdbc.intro.v1.SimpleInsert}.
*
* @param args unused
* @throws SQLException possible causes:
* <ul>
* <li>Driver matching JDBC URL unavailable</li>
* <li>No network access to database server</li>
* <li>Invalid credentials</li>
* <li>Database table 'Person' does not exist</li>
* <li>duplicate email (UNIQUE key)</li>
* <li>... something forgotten?</li>
* </ul>
*/
public static void main(String[] args) throws SQLException {
// Step 1: Open a connection to the database server
final Connection conn = DriverManager.getConnection (
"jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC&useSSL=true", //JDBC URL
"hdmuser", // Username
"XYZ"); // Password
// Step 2: Create a Statement instance
final Statement stmt = conn.createStatement();
// Step 3: Execute the desired INSERT
final int updateCount = stmt.executeUpdate(
"INSERT INTO Person VALUES('Jim', 'jim@foo.org')");
// Step 4: Give feedback to the end user
System.out.println("Successfully inserted " + updateCount + " dataset(s)");
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment