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;
/*
* 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 void main(String[] args){
DBConnection db = new DBConnection();

View File

@ -23,19 +23,27 @@ public class UserDAO {
public UserDAO() {
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(){
ArrayList<User> users = new ArrayList<User>();
PreparedStatement ps;
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();
while (rs.next()) {
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);
users.add(user);
users.add(getUserFromResultSet(rs));
}
return users;
} catch (SQLException e) {
@ -51,19 +59,14 @@ public class UserDAO {
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
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;
return getUserFromResultSet(rs);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public List<User> findByName(String name){
return null;
}
public boolean insertUser(User User){
return false;
}

View File

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