Add tests, db connection

This commit is contained in:
Tyrel Souza 2017-02-28 00:08:01 -05:00
parent b1a8cac64c
commit 5260dcfb81
No known key found for this signature in database
GPG Key ID: 2EECB5087209E6A5
4 changed files with 50 additions and 16 deletions

View File

@ -9,6 +9,5 @@ import db.DBConnection;
public class HourTracker {
public void main(String[] args){
DBConnection db = new DBConnection();
db.connect();
}
}

View File

@ -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<User> findAll(){
DBConnection db;
public UserDAO() {
db = new DBConnection();
}
public List<User> findAll(){
return null;
}
List<User> 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<User> findByName(String name){
public List<User> 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;
}

View File

@ -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());

View File

@ -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());
}
}