Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class Main {
- public static void main(String[] args) {
- try{
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Can't find driver");
- }
- Connection connection = null;
- try {
- connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "948200");
- } catch (SQLException e){
- e.printStackTrace();
- }
- if (connection != null) {
- // try {
- // ResultSet rs = connection.createStatement().executeQuery("SELECT count(*) FROM \"user\"");
- // rs.next();
- // System.out.println(rs.getString(1));
- // } catch (SQLException e) {
- // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- // }
- //add lots of records to table
- // for (int i = 0; i < 1000000; i++) {
- // String sql_no_indexes = new StringBuilder("INSERT INTO test_users_without_index VALUES (").append(i).
- // append(",").append(i).append(",").append(i).append(", '1991-11-11')").toString();
- // String sql_indexes = new StringBuilder("INSERT INTO test_users_with_index VALUES (").append(i).
- // append(",").append(i).append(",").append(i).append(", '1991-11-11')").toString();
- // try {
- // System.out.println(i);
- // connection.createStatement().executeUpdate(sql_no_indexes);
- // connection.createStatement().executeUpdate(sql_indexes);
- // } catch (SQLException e) {
- // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- // }
- // }
- //try to read data
- final Connection cnx = connection;
- long result = getMills(new Runnable() {
- public void run() {
- try {
- ResultSet resultSet = cnx.createStatement().
- executeQuery("SELECT DISTINCT * FROM test_users_without_index WHERE login = '555555'");
- resultSet.next();
- } catch (SQLException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- });
- System.out.println("Search without index");
- System.out.println(result);
- //try to read data
- result = getMills(new Runnable() {
- public void run() {
- try {
- ResultSet resultSet = cnx.createStatement().executeQuery("SELECT DISTINCT * " +
- "FROM test_users_with_index WHERE login = '555555'");
- resultSet.next();
- } catch (SQLException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- });
- System.out.println("Search with index");
- System.out.println(result);
- // try to update data
- result = getMills(new Runnable() {
- public void run() {
- try {
- cnx.createStatement().executeUpdate("INSERT INTO test_users_without_index VALUES (1000002, " +
- "'111111113', 'password', '1991-01-01')");
- } catch (SQLException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- });
- System.out.println("Insert without index");
- System.out.println(result);
- // try to update data
- result = getMills(new Runnable() {
- public void run() {
- try {
- cnx.createStatement().executeUpdate("INSERT INTO test_users_with_index VALUES (1000001, " +
- "'111111113', 'password', '1991-01-01')");
- } catch (SQLException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- });
- System.out.println("Insert with index");
- System.out.println(result);
- // delete with index
- result = getMills(new Runnable() {
- public void run() {
- try {
- cnx.createStatement().executeUpdate("DELETE FROM test_users_without_index WHERE id=1000001");
- } catch (SQLException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- });
- System.out.println("Delete without index");
- System.out.println(result);
- // delete
- result = getMills(new Runnable() {
- public void run() {
- try {
- cnx.createStatement().executeUpdate("DELETE FROM test_users_with_index WHERE id=1000001");
- } catch (SQLException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- });
- System.out.println("Delete with index");
- System.out.println(result);
- }
- }
- private static long getMills(Runnable r) {
- long start = System.currentTimeMillis();
- r.run();
- long end = System.currentTimeMillis();
- return end - start;
- }
- }
Add Comment
Please, Sign In to add comment