refactoring

This commit is contained in:
Tyrel Souza 2017-02-28 21:04:27 -05:00
parent a832375223
commit e5a3fafa09
No known key found for this signature in database
GPG Key ID: 2EECB5087209E6A5
3 changed files with 17 additions and 23 deletions

View File

@ -1,11 +1,5 @@
import db.DBConnection; import db.DBConnection;
/*
* This Java source file was auto generated by running 'gradle buildInit --type java-library'
* by 'tyrelsouza' at '2/27/17 9:23 PM' with Gradle 2.10
*
* @author tyrelsouza, @date 2/27/17 9:23 PM
*/
public class HourTracker { public class HourTracker {
public void main(String[] args){ public void main(String[] args){
DBConnection db = new DBConnection(); DBConnection db = new DBConnection();

View File

@ -23,19 +23,27 @@ public class UserDAO {
public UserDAO() { public UserDAO() {
db = new DBConnection(); db = new DBConnection();
} }
private User getUserFromResultSet(ResultSet rs){
try {
Integer userid = Integer.parseInt(rs.getString("id"));
String username = rs.getString("username");
String fullName = rs.getString("full_name");
User user = new User(userid, username, fullName);
return user;
} catch (SQLException e) {
return null;
}
}
public ArrayList<User> findAll(){ public ArrayList<User> findAll(){
ArrayList<User> users = new ArrayList<User>(); ArrayList<User> users = new ArrayList<User>();
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = db.connection.prepareStatement("SELECT id, username, full_name FROM user"); ps = db.connection.prepareStatement(
"SELECT id, username, full_name FROM user");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
while (rs.next()) { while (rs.next()) {
Integer userid = Integer.parseInt(rs.getString("id")); users.add(getUserFromResultSet(rs));
String username = rs.getString("username");
String fullName = rs.getString("full_name");
User user = new User(userid, username, fullName);
users.add(user);
} }
return users; return users;
} catch (SQLException e) { } catch (SQLException e) {
@ -51,19 +59,14 @@ public class UserDAO {
ps.setInt(1, id); ps.setInt(1, id);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
rs.next(); rs.next();
Integer userid = Integer.parseInt(rs.getString("id"));
String username = rs.getString("username"); return getUserFromResultSet(rs);
String fullName = rs.getString("full_name");
User user = new User(userid, username, fullName);
return user;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
public List<User> findByName(String name){
return null;
}
public boolean insertUser(User User){ public boolean insertUser(User User){
return false; return false;
} }

View File

@ -4,9 +4,6 @@ import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
/**
* Created by tyrelsouza on 2/27/17.
*/
public class DBConnection { public class DBConnection {
public static Connection connection = null; public static Connection connection = null;