Back to project page GasTracker.
The source code is released under:
Copyright 2014 kurtzy317
If you think the Android project GasTracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.mindalsoblown.gastracker; //from www .j a v a 2s. c o m import java.io.FileInputStream; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import android.app.AlertDialog; import android.os.StrictMode; public class DatabaseConnector { private Connection connect; private int UserID; public int getUserID() { return UserID; } public void setUserID(int userID) { UserID = userID; } public DatabaseConnector(FileInputStream fi) { Initialize(); Scanner scan = new Scanner(fi); UserID = Integer.parseInt(scan.next()); scan.close(); } public DatabaseConnector() { Initialize(); } public void Initialize () { //allow network call on main thread. BAD IDEA!!!!! StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); //attempt initialization of database connection try { Class.forName("com.mysql.jdbc.Driver"); connect = DriverManager.getConnection("jdbc:mysql://www.jordankurtz.com:3306/FuelData", "Gas", "2r#PYFP8Z8"); System.out.write(' '); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //add new entry to database as a new thread public void AddEntry (final Date date, final int TotalMiles, final double TripMiles, final double CarMPGs, final double Gallons) {new Thread(new Runnable() { public void run() { try { double calculateMPGs = TripMiles/Gallons; double carMPGs = CarMPGs; if (CarMPGs == 0) carMPGs = calculateMPGs; double averageMPGs = (calculateMPGs + carMPGs)/2; PreparedStatement preparedStatement; preparedStatement = connect.prepareStatement("insert into FuelData.Data (Date, Gallons, Car_MPG, Calculated_MPG, Miles_On_Tank, Mileage, Average_MPG, UserID) values (?,?,?,?,?,?,?,?)"); preparedStatement.setDate(1, date); preparedStatement.setDouble(2, Gallons); preparedStatement.setDouble(3, CarMPGs); preparedStatement.setDouble(4, calculateMPGs); preparedStatement.setDouble(5, TripMiles); preparedStatement.setInt(6, TotalMiles); preparedStatement.setDouble(7, averageMPGs); preparedStatement.setInt(8, UserID); preparedStatement.executeUpdate(); } catch (Exception ex) { AlertDialog.Builder exception = new AlertDialog.Builder(null); exception.setMessage(ex.getMessage()); exception.create().show(); } } }).start(); } //get a previous entry by the ID public Entry GetEntryById(int ID) { Entry entry = new Entry(); ResultSet results; try { Statement statement = connect.createStatement(); results = statement.executeQuery("SELECT * FROM FuelData.Data where ID = '" + ID + "'"); results.next(); entry.setAverageMPG(results.getDouble("Average_MPG")); entry.setCalculatedMPG(results.getDouble("Calculated_MPG")); entry.setCarMPG(results.getDouble("Car_MPG")); entry.setDate(results.getDate("Date")); entry.setGallons(results.getDouble("Gallons")); entry.setTotalMiles(results.getDouble("Mileage")); entry.setTripMiles(results.getDouble("Miles_On_Tank")); entry.setId(results.getInt("ID")); } catch (Exception e) { } return entry; } //get history for the specific user public List<Entry> getHistoryForUser () { List<Entry> entries = new ArrayList<Entry>(); try { ResultSet results; Statement statement = connect.createStatement(); results = statement.executeQuery("SELECT * FROM FuelData.Data Where UserID = '" + UserID + "'"); while (results.next()) { Entry entry = new Entry(); entry.setAverageMPG(results.getDouble("Average_MPG")); entry.setCalculatedMPG(results.getDouble("Calculated_MPG")); entry.setCarMPG(results.getDouble("Car_MPG")); entry.setDate(results.getDate("Date")); entry.setGallons(results.getDouble("Gallons")); entry.setTotalMiles(results.getDouble("Mileage")); entry.setTripMiles(results.getDouble("Miles_On_Tank")); entry.setId(results.getInt("ID")); entries.add(entry); } } catch (Exception ex) { System.out.print(ex.toString()); } return entries; } public Entry GetAveragesForUser() { Entry entry = new Entry(); ResultSet results; try { Statement statement = connect.createStatement(); results = statement.executeQuery("SELECT * FROM FuelData.Averages Where `User ID` = '" + UserID + "'"); results.next(); entry.setAverageMPG(results.getDouble("Average MPG")); entry.setCalculatedMPG(results.getDouble("Calculated MPG")); entry.setCarMPG(results.getDouble("Car MPG")); entry.setGallons(results.getDouble("Gallons")); entry.setTripMiles(results.getDouble("Miles On Tank")); } catch (Exception ex) { System.out.print(ex.toString()); } return entry; } public void Register (final User user) {new Thread(new Runnable() { public void run() { try { PreparedStatement preparedStatement; preparedStatement = connect.prepareStatement("insert into FuelData.Users (FName, LName, Email, Password, UserName, CarMPG) values (?,?,?,?,?,?)"); preparedStatement.setString(1, user.getFName()); preparedStatement.setString(2, user.getLName()); preparedStatement.setString(3, user.getEmail()); preparedStatement.setString(4, user.getPassword()); preparedStatement.setString(5, user.getUserName()); preparedStatement.setBoolean(6, user.isCarMPG()); preparedStatement.executeUpdate(); } catch (Exception ex) { AlertDialog.Builder exception = new AlertDialog.Builder(null); exception.setMessage(ex.getMessage()); exception.create().show(); } } }).start(); } public User GetUserByUserName(String UserName) { User user = new User(); ResultSet results; try { Statement statement = connect.createStatement(); results = statement.executeQuery("SELECT * FROM FuelData.Users where UserName = '" + UserName + "'"); results.next(); user.setCarMPG(results.getBoolean("CarMPG")); user.setEmail(results.getString("Email")); user.setFName(results.getString("FName")); user.setID(results.getInt("ID")); user.setLName(results.getString("LName")); user.setPassword(results.getString("Password")); user.setUserName(results.getString("UserName")); } catch (Exception ex) { System.out.print(ex.toString()); } return user; } public User GetUserByID() { User user = new User(); ResultSet results; try { Statement statement = connect.createStatement(); results = statement.executeQuery("SELECT * FROM FuelData.Users where ID = '" + UserID + "'"); results.next(); user.setCarMPG(results.getBoolean("CarMPG")); user.setEmail(results.getString("Email")); user.setFName(results.getString("FName")); user.setID(results.getInt("ID")); user.setLName(results.getString("LName")); user.setPassword(results.getString("Password")); user.setUserName(results.getString("UserName")); } catch (Exception ex) { System.out.print(ex.toString()); } return user; } public List<String> GetAllUserNames () { List<String> userNames = new ArrayList<String>(); ResultSet results; try { Statement statement = connect.createStatement(); results = statement.executeQuery("Select UserName from Users"); while (results.next()) { userNames.add(results.getString("UserName")); } } catch (Exception ex) { } return userNames; } public List<String> GetAllEmails () { List<String> userNames = new ArrayList<String>(); ResultSet results; try { Statement statement = connect.createStatement(); results = statement.executeQuery("Select Email from Users"); while (results.next()) { userNames.add(results.getString("Email")); } } catch (Exception ex) { } return userNames; } public boolean GetUserSetting(String setting) { ResultSet results; try { Statement statement = connect.createStatement(); results = statement.executeQuery("SELECT "+setting+" FROM FuelData.Users where ID = '" + UserID + "'"); results.next(); return results.getBoolean(1); } catch (Exception ex) { } return false; } }