Initial commit
This commit is contained in:
3
Java/RandomGenerator/.idea/.gitignore
generated
vendored
Normal file
3
Java/RandomGenerator/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
9
Java/RandomGenerator/.idea/artifacts/RandomGenerator_jar.xml
generated
Normal file
9
Java/RandomGenerator/.idea/artifacts/RandomGenerator_jar.xml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<component name="ArtifactManager">
|
||||
<artifact type="jar" name="RandomGenerator:jar">
|
||||
<output-path>$PROJECT_DIR$/out/artifacts/RandomGenerator_jar</output-path>
|
||||
<root id="archive" name="RandomGenerator.jar">
|
||||
<element id="module-output" name="RandomGenerator" />
|
||||
<element id="extracted-dir" path="$PROJECT_DIR$/../MySQL Connector.jar" path-in-jar="/" />
|
||||
</root>
|
||||
</artifact>
|
||||
</component>
|
||||
9
Java/RandomGenerator/.idea/libraries/MySQL_Connector.xml
generated
Normal file
9
Java/RandomGenerator/.idea/libraries/MySQL_Connector.xml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<component name="libraryTable">
|
||||
<library name="MySQL Connector">
|
||||
<CLASSES>
|
||||
<root url="jar://$PROJECT_DIR$/../MySQL Connector.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
||||
6
Java/RandomGenerator/.idea/misc.xml
generated
Normal file
6
Java/RandomGenerator/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
Java/RandomGenerator/.idea/modules.xml
generated
Normal file
8
Java/RandomGenerator/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/RandomGenerator.iml" filepath="$PROJECT_DIR$/RandomGenerator.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
12
Java/RandomGenerator/RandomGenerator.iml
Normal file
12
Java/RandomGenerator/RandomGenerator.iml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" exported="" name="MySQL Connector" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: de.craftix.generator.Generator
|
||||
|
||||
Binary file not shown.
Binary file not shown.
3
Java/RandomGenerator/src/META-INF/MANIFEST.MF
Normal file
3
Java/RandomGenerator/src/META-INF/MANIFEST.MF
Normal file
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: de.craftix.generator.Generator
|
||||
|
||||
40
Java/RandomGenerator/src/de/craftix/generator/Generator.java
Normal file
40
Java/RandomGenerator/src/de/craftix/generator/Generator.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package de.craftix.generator;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Random;
|
||||
|
||||
public class Generator {
|
||||
public static MySQL sql;
|
||||
public static int id = 0;
|
||||
|
||||
public static void main(String[] args) {
|
||||
//TODO: Add MySQL Connection
|
||||
sql = new MySQL("server.craftgang.de", 3306, "randomiser", "randomiser", "Kz.iJfkEmi.EBu2O");
|
||||
sql.connect();
|
||||
sql.insert("CREATE TABLE IF NOT EXISTS Results (ID INT(10), Timestamp VARCHAR(50), Number VARCHAR(100), Trys BIGINT(255))");
|
||||
startRandomLoop();
|
||||
}
|
||||
|
||||
private static void startRandomLoop() {
|
||||
long trys = 0;
|
||||
Random rand = new Random();
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
float value = rand.nextFloat();
|
||||
System.out.print(value + "; ");
|
||||
if (value > 0.9999999f) {
|
||||
success(trys, value);
|
||||
trys = 0;
|
||||
id++;
|
||||
}
|
||||
trys++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void success(long trys, float number) {
|
||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
sql.insert("INSERT INTO Results (ID, Timestamp, Trys, Number) VALUES (" + id + ", \"" + dtf.format(now) + "\", " + trys + ", \"" + number + "\")");
|
||||
}
|
||||
|
||||
}
|
||||
62
Java/RandomGenerator/src/de/craftix/generator/MySQL.java
Normal file
62
Java/RandomGenerator/src/de/craftix/generator/MySQL.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package de.craftix.generator;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class MySQL implements Serializable {
|
||||
protected String server;
|
||||
protected int port;
|
||||
protected String database;
|
||||
protected String username;
|
||||
protected String password;
|
||||
protected Connection con;
|
||||
|
||||
public MySQL(String server, int port, String database, String username, String password) {
|
||||
this.server = server;
|
||||
this.port = port;
|
||||
this.database = database;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public MySQL(String server, String database, String username, String password) { this(server, 3306, database, username, password); }
|
||||
|
||||
public void connect() {
|
||||
if (isConnected()) return;
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + database, username, password);
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
if (!isConnected()) return;
|
||||
try {
|
||||
con.close();
|
||||
con = null;
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public boolean isConnected() { return con != null; }
|
||||
|
||||
public void insert(String qry) {
|
||||
if (!isConnected()) throw new NullPointerException("MySQL not connected");
|
||||
try {
|
||||
con.prepareStatement(qry).executeUpdate();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public ResultSet getData(String qry) {
|
||||
if (!isConnected()) throw new NullPointerException("MySQL not connected");
|
||||
try {
|
||||
return con.prepareStatement(qry).executeQuery();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user