From 5260dcfb8144812438ebe525e6ac2393c17cc19d Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 28 Feb 2017 00:08:01 -0500 Subject: [PATCH] Add tests, db connection --- src/main/java/HourTracker.java | 1 - src/main/java/dao/UserDAO.java | 39 ++++++++++++++++++++++++------ src/main/java/db/DBConnection.java | 8 +++--- src/test/java/HourTrackerTest.java | 18 ++++++++++---- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/main/java/HourTracker.java b/src/main/java/HourTracker.java index e7529d9..a887a63 100644 --- a/src/main/java/HourTracker.java +++ b/src/main/java/HourTracker.java @@ -9,6 +9,5 @@ import db.DBConnection; public class HourTracker { public void main(String[] args){ DBConnection db = new DBConnection(); - db.connect(); } } diff --git a/src/main/java/dao/UserDAO.java b/src/main/java/dao/UserDAO.java index aac8a48..4c37d1f 100644 --- a/src/main/java/dao/UserDAO.java +++ b/src/main/java/dao/UserDAO.java @@ -1,34 +1,59 @@ package dao; +import db.DBConnection; import model.User; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.List; /** * Created by tyrelsouza on 2/27/17. - CREATE TABLE entry ( + CREATE TABLE user ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `username` varchar(100) not null, `full_name` varchar(200) not null ); */ public class UserDAO { - List findAll(){ + DBConnection db; + public UserDAO() { + db = new DBConnection(); + } + + public List findAll(){ return null; } - List findById(int id){ + public User getById(int id){ + PreparedStatement ps; + try { + ps = db.connection.prepareStatement( + "SELECT id, username, full_name FROM user where id = ? LIMIT 1"); + 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; + } catch (SQLException e) { + e.printStackTrace(); + } return null; } - List findByName(String name){ + public List findByName(String name){ return null; } - boolean insertUser(User User){ + public boolean insertUser(User User){ return false; } - boolean updateUser(User User){ + public boolean updateUser(User User){ return false; } - boolean deleteUser(User User){ + public boolean deleteUser(User User){ return false; } diff --git a/src/main/java/db/DBConnection.java b/src/main/java/db/DBConnection.java index 0e1d85b..87ebcd8 100644 --- a/src/main/java/db/DBConnection.java +++ b/src/main/java/db/DBConnection.java @@ -8,10 +8,12 @@ import java.sql.SQLException; * Created by tyrelsouza on 2/27/17. */ public class DBConnection { - public static Connection conn = null; - public static void connect(){ + public static Connection connection = null; + + public DBConnection() { try { - conn = DriverManager.getConnection("jdbc:mysql://localhost/hourtracker?user=hourtracker&password=hours"); +// conn = DriverManager.getConnection("jdbc:mysql://localhost/hourtracker?user=hourtracker&password=hours"); + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hourtracker","hourtracker","hours"); } catch (SQLException e) { System.out.println("SQLException: " + e.getMessage()); System.out.println("SQLState: " + e.getSQLState()); diff --git a/src/test/java/HourTrackerTest.java b/src/test/java/HourTrackerTest.java index c3c352d..dbf03d2 100644 --- a/src/test/java/HourTrackerTest.java +++ b/src/test/java/HourTrackerTest.java @@ -1,15 +1,15 @@ +import dao.UserDAO; import db.DBConnection; + import junit.framework.Assert; import model.User; +import org.junit.Before; import org.junit.Test; +import java.util.List; -import static junit.framework.Assert.assertTrue; /* - * This Java source file was auto generated by running 'gradle init --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 HourTrackerTest { @@ -27,6 +27,14 @@ public class HourTrackerTest { @Test public void testDBConnection(){ DBConnection db = new DBConnection(); - db.connect(); + Assert.assertNotNull(db.connection); + } + + @Test + public void testUserDAO(){ + UserDAO userDao = new UserDAO(); + User user = userDao.getById(1); + Assert.assertEquals("tyrel", user.getUsername()); + } }