/* * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED. * Use of this software is authorized pursuant to the terms of the license found at * http://developer.java.sun.com/berkeley_license.html. */ import java.io.BufferedReader; import java.io.FileReader; import java.sql.*; import java.util.StringTokenizer; public class Lab11 { public static void main(String args[]) { //Set connection info. //TODO Replace with the name of your empty database String url = "jdbc:sqlite:/44907087_P2"; Connection con; Statement ddl, stmt, stmt2; PreparedStatement pstmt; //Load the driver class try { Class.forName("SQLite.JDBCDriver"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { //Create a connection to the db con = DriverManager.getConnection(url, "myLogin", "myPassword"); //Create tables ddl = con.createStatement(); //TODO SQL DDL for creating our tables. Refer to assignment for schema. String createPlayerGames = "CREATE TABLE playerGames ( pid INTEGER, gid INTEGER, score INTEGER);"; String createTopScores = "CREATE TABLE topScores ( pid INTEGER, gid INTEGER, score INTEGER);"; ddl.executeUpdate(createPlayerGames); ddl.executeUpdate(createTopScores); //Read in player_games.txt content and insert into the database. try{ BufferedReader br = new BufferedReader(new FileReader("player_games.txt")); String strLine; int linenum = 1; //This is the total number of lines to read from player_games.txt. //If you attempt to read the entire file (10000 lines) you will be waiting for about an hour. //For the lab we will read 1000 lines only, which will still take a few minutes. int totalLines = 1000; //TODO Declare the prepared statement for inserting data into playerGames table. //Refer to java.sql.PreparedStatement javadoc for more info. String preparedInsertStatement = "INSERT INTO playerGames (pid, gid, score) VALUES (?,?,?);"; pstmt = con.prepareStatement(preparedInsertStatement); // PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?"); //pstmt.setBigDecimal(1, 153833.00) //pstmt.setInt(2, 110592) //Read File Line By Line System.out.println("Building playerGames table from file:"); double start = System.currentTimeMillis(); System.out.print("| | 0%\r"); while ((strLine = br.readLine()) != null && linenum<=totalLines) { //TODO: Extract values from the line read. StringTokenizer tkn = new StringTokenizer(strLine, "\t"); String pid = tkn.nextToken(); String gid = tkn.nextToken(); String scr = tkn.nextToken(); //TODO: Set the values in the prepared statement to the values read from the file. pstmt.setString(1, pid); pstmt.setString(2, gid); pstmt.setString(3, scr); //Execute the prepared statement. pstmt.executeUpdate(); //Update progress bar int bars = linenum/(totalLines/10); int percent = linenum/totalLines*100; String bar = "|"; for(int i=0; i<10; i++) { bar += (i