Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Random;
- /*
- * @author Josef
- *
- */
- public class Main {
- public static void main(String[] args) {
- try {
- Connection con = DriverManager.getConnection(
- "jdbc:mysql://127.0.0.1:3308/test", "root", "password");
- PreparedStatement statement;
- ResultSet res;
- ArrayList<TestObject> locations = new ArrayList<TestObject>();
- ArrayList<TestObject> fetchedLocations;
- String[] worlds = { "Bobs World", "Potatoland", "Baconcity",
- "Cocoa Bean", "Feelsville", "Sweden" };
- long time;
- long doneTime;
- int times = 1;
- /* Creating 100 random made-up cords before starting test */
- for (int c = 0; c < times; c++) {
- Random r = new Random();
- locations.add(new TestObject(r.nextDouble() * 20000 - 10000, r
- .nextDouble() * 20000 - 10000,
- r.nextDouble() * 20000 - 10000, worlds[r.nextInt(5)], r
- .nextFloat() * 360 - 180, r.nextFloat() * 180));
- }
- for (int i = 0; i < 5; i++) {
- /* Clear tables from previous tries */
- statement = con
- .prepareStatement("DELETE FROM othertest WHERE 1;");
- statement.executeUpdate();
- statement = con
- .prepareStatement("DELETE FROM testtable WHERE 1;");
- statement.executeUpdate();
- /* Saving then fetching as combined String */
- fetchedLocations = new ArrayList<TestObject>();
- System.out.println("Starting combined at "
- + System.currentTimeMillis());
- time = System.currentTimeMillis();
- for (int c = 0; c < times; c++) {
- TestObject l = locations.get(c);
- statement = con
- .prepareStatement("INSERT INTO othertest (loc) VALUES (?);");
- statement.setString(1, l.x + ":" + l.y + ":" + l.z + ":"
- + l.w + ":" + l.ya + ":" + l.p);
- statement.executeUpdate();
- }
- for (int c = 0; c < times; c++) {
- statement = con.prepareStatement("SELECT * FROM othertest");
- res = statement.executeQuery();
- while (res.next()) {
- String[] a = res.getString("loc").split(":");
- fetchedLocations.add(new TestObject(Double
- .parseDouble(a[0]), Double.parseDouble(a[1]),
- Double.parseDouble(a[2]), a[3], Float
- .parseFloat(a[4]), Float
- .parseFloat(a[5])));
- }
- }
- doneTime = System.currentTimeMillis();
- System.out.println("Stopped combined at "
- + System.currentTimeMillis() + " (Total of "
- + (doneTime - time) + ")");
- /* Saving then fetching as individual variables */
- fetchedLocations = new ArrayList<TestObject>();
- System.out.println("Starting individual at "
- + System.currentTimeMillis());
- time = System.currentTimeMillis();
- for (int c = 0; c < times; c++) {
- TestObject l = locations.get(c);
- statement = con
- .prepareStatement("INSERT INTO testtable (x, y, z, world, yaw, pitch) VALUES (?, ?, ?, ?, ?, ?);");
- statement.setString(1, l.x + "");
- statement.setString(2, l.y + "");
- statement.setString(3, l.z + "");
- statement.setString(4, l.w);
- statement.setString(5, l.ya + "");
- statement.setString(6, l.p + "");
- statement.executeUpdate();
- }
- for (int c = 0; c < times; c++) {
- statement = con.prepareStatement("SELECT * FROM testtable");
- res = statement.executeQuery();
- while (res.next()) {
- fetchedLocations
- .add(new TestObject(res.getDouble("x"), res
- .getDouble("y"), res.getDouble("z"),
- res.getString("world"), res
- .getFloat("yaw"), res
- .getFloat("pitch")));
- }
- }
- doneTime = System.currentTimeMillis();
- System.out.println("Stopped individual at "
- + System.currentTimeMillis() + " (Total of "
- + (doneTime - time) + ")");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- class TestObject {
- public double x, y, z;
- public float ya, p;
- public String w;
- TestObject(double x, double y, double z, String w, float ya, float p) {
- this.x = x;
- this.y = y;
- this.z = z;
- this.w = w;
- this.ya = ya;
- this.p = p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement