简体   繁体   中英

Make this program more efficient?

just curious if anyone has any idea for making this program more simple. It reads records from a database into an ArrayList and allows the user to search for records by state. It processes a database of 1 million records in aprox 16000ms.

import java.sql.*;
import java.util.*;
public class ShowEmployeeDB
{
public static void main(String args[])     
{
   ArrayList <String> Recs = new ArrayList <String>();
   String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
   String connectionURL = "jdbc:odbc:CitizensDB";  
   Connection con = null;    
   Statement  stmt = null;     
   String sqlStatement = "SELECT * FROM Citizens";
   ResultSet rs = null;       
   int r = 0;
   Scanner scan = new Scanner(System.in);
   String search = null;
   long starttime = System.currentTimeMillis();
   try 
   {   
       Class.forName(driverName).newInstance();  
       con = DriverManager.getConnection(connectionURL);
       stmt = con.createStatement();    
       rs = stmt.executeQuery(sqlStatement);  

       String ID = null;
       String Age = null;
       String State = null;
       String Gender = null;
       String Status = null;
       String record = null;
       while (rs.next())
       {
           for (int k = 1; k <= 1; ++k)
           {
               ID = rs.getString(k) + " ";
               for (int j = 2; j <= 2; ++j)
                   Age = rs.getString(j) + " ";
               for (int i = 3; i <= 3; ++i)
                   State = rs.getString(i).toUpperCase() + " ";
               for (int h = 4; h <= 4; ++h)
                   Gender = rs.getString(h) + " ";
               for (int g = 5; g <= 5; ++g)
                   Status = rs.getString(g) + " ";
           }//for
           record = ID + Age + State + Gender + Status;
           Recs.add(record);
           ++r;
       }//while
       rs.close();
       stmt.close();
       con.close();
   } catch (Exception ex) { ex.printStackTrace();  }

   String endtime = System.currentTimeMillis() - starttime + "ms";
   System.out.println(endtime);

   System.out.print("Enter A Search State: ");
   search = scan.nextLine().toUpperCase();

   Iterator<String> iter = Recs.iterator();
    while(iter.hasNext())
    {
        String s = iter.next();
        if (s.contains(search))
        {
            System.out.println(s);
        }
    }//while
} // main
} // ShowEmployeeBD

Any advice would be greatly appreciated. Thank you in advance!

If search is not often, I would suggest to take the search string input before running the query, so that search results are directly from the DB. I this case you do not have to reiterate all 1 million records.

Perform searching directly on DB rather than fetching all the records and searching through java code. Also if search is on multiple column, then prepare a meta data in DB at a single place on the basis of IDs, and the meta data can further be used for fetching the required results that match the query.

  • Separate your logic from the technical stuff. In such a convolut it is difficult to run unit tests or any optimizations.
  • Why do you need for loops, when only asking one value.
  • Use StringBuilder instead of String concatenation.
  • Use either try-with or put your close statements in a finally clause.
  • Don't initialize variables you don't need (r).
  • Use for each statements.
  • Query the database, not the result set.
  • Tune your database.
  • If you are only searching for a state, filter only those, so build an object and compare the state instead of a string contains.
  • Compare the state before storing strings in the list.
  • Tune your list because it constantly grows with 1Mio records.
  • Use a hashset instead of an arraylist.
  • Develop against interfaces.

A better program might look like following:

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class ShowEmployeeDB {

  private static final String DRIVERNAME = "sun.jdbc.odbc.JdbcOdbcDriver";
  private static final String CONNECTIONURL = "jdbc:odbc:CitizensDB";

  private static final String SELECT_CITIZENS = "SELECT * FROM Citizens";

  static {
    try {
      DriverManager.registerDriver((Driver) Class.forName(DRIVERNAME).newInstance());
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
      e.printStackTrace();
    }
  }

  public static void main(final String args[]) {
    System.out.print("Enter A Search State: ");
    searchRecords();
  }

  private static void searchRecords() {
    try(Scanner scan = new Scanner(System.in);) {
      final String state = scan.nextLine();

      final long starttime = System.currentTimeMillis();
      final Set<Record> records = searchRecordsByState(state);
      System.out.println(System.currentTimeMillis() - starttime + "ms");

      for(final Record r : records) {
         System.out.println(r);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static Set<Record> searchRecordsByState(final String stateToFilter) {
    final Set<Record> records = new HashSet<>();
    try(Connection con = DriverManager.getConnection(CONNECTIONURL);
        PreparedStatement stmt = con.prepareStatement(SELECT_CITIZENS);
        ResultSet rs = stmt.executeQuery(); ) {

      while(rs.next()) {
        final String state = rs.getString(3);
        if(state.equalsIgnoreCase(stateToFilter)) {
          final Record r = new Record(rs.getString(1), rs.getString(2), state, rs.getString(4), rs.getString(5));
          records.add(r);
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return records;
  }
}

class Record {
  String id, age, state, gender, status;

  public Record(String id, String age, String state, String gender, String status) {
    this.id = id;
    this.age = age;
    this.state = state;
    this.gender = gender;
    this.status = status;
  }

  public String getState() {
    return state;
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(id).append(' ')
      .append(age).append(' ')
      .append(state).append(' ')
      .append(gender).append(' ')
      .append(status);
    return sb.toString();
  }
}

This is untested, because I don't have a database with a million entries by hand.

But the best would be to query the database and catch only those entries you need. So use the WHERE -clause in your statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM