Skip to content

completed lab #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

Secrets.java
# Created by https://www.gitignore.io/api/intellij
# Edit at https://www.gitignore.io/?templates=intellij

Expand Down
6 changes: 4 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

5 changes: 2 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions jdbcdao.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.18" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.6.1" level="project" />
</component>
</module>
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>
</project>
35 changes: 35 additions & 0 deletions src/main/java/daos/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package daos;

import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//Connects to Database
public class ConnectionFactory {

public static final String URL = Secrets.getURL();
public static final String USER = Secrets.getUSER();
public static final String PASS = Secrets.getPASS();

//Get a connection to database
public static Connection getConnection() {

try {
DriverManager.registerDriver(new Driver());
return DriverManager.getConnection(URL, USER, PASS);
} catch (SQLException ex) {
throw new RuntimeException("Error connecting to the database", ex);
}
}

//test Connection
public static void main(String[] args) {
Connection connection = ConnectionFactory.getConnection();
MovieDAO mdao = new MovieDAO();
// for (int i = 7; i < 14; i++) {
// mdao.delete(i);
// }
}
}
11 changes: 11 additions & 0 deletions src/main/java/daos/DAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package daos;

import java.util.Set;

public interface DAO<T> {
T findById(Integer id);
Set<T> findAll();
Boolean update(Integer id, T object);
Boolean create(T object);
Boolean delete(Integer id);
}
Empty file removed src/main/java/daos/DELETEME.txt
Empty file.
5 changes: 5 additions & 0 deletions src/main/java/daos/DTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package daos;

public interface DTO<T> {
int getId(T object);
}
114 changes: 114 additions & 0 deletions src/main/java/daos/MovieDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package daos;

import models.Movie;

import java.sql.*;
import java.util.HashSet;
import java.util.Set;

public class MovieDAO implements DAO<Movie> {

public Movie findById(Integer id) {
Connection conn = ConnectionFactory.getConnection();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Movies WHERE id=" + id);

if (rs.next()) {
return extractMovieFromResultSet(rs);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}

public Set<Movie> findAll() {
Connection conn = ConnectionFactory.getConnection();

try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Movies");
Set movies = new HashSet();

while(rs.next())
{
Movie movie = extractMovieFromResultSet(rs);

movies.add(movie);
}
return movies;
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}

public Boolean update(Integer id, Movie movie) {
Connection conn = ConnectionFactory.getConnection();

try{
PreparedStatement ps = conn.prepareStatement("UPDATE Movies SET title=?, runtime=?, imdbscore=?, releaseyear=? WHERE id=?;");
ps.setString(1, movie.getTitle());
ps.setInt(2, movie.getRuntime());
ps.setInt(3, movie.getImdbscore());
ps.setInt(4, movie.getReleaseyear());
ps.setInt(5, id);
int i = ps.executeUpdate();

if(i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}

public Boolean create(Movie movie) {
Connection conn = ConnectionFactory.getConnection();

try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO Movies VALUES (NULL, ?, ?, ?, ?);");
ps.setString(1, movie.getTitle());
ps.setInt(2, movie.getRuntime());
ps.setInt(3, movie.getImdbscore());
ps.setInt(4, movie.getReleaseyear());
int i = ps.executeUpdate();

if(i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}

public Boolean delete(Integer id) {
Connection conn = ConnectionFactory.getConnection();

try {

Statement stmt = conn.createStatement();
int i = stmt.executeUpdate("DELETE FROM Movies WHERE id=" + id);

if(i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}

private Movie extractMovieFromResultSet(ResultSet rs) throws SQLException {
Movie movie = new Movie();
movie.setId( rs.getInt("id") );
movie.setTitle( rs.getString("title") );
movie.setRuntime( rs.getInt("runtime") );
movie.setImdbscore( rs.getInt("imdbscore") );
movie.setReleaseyear( rs.getInt("releaseyear") );
return movie;
}
}
26 changes: 26 additions & 0 deletions src/main/java/daos/MovieDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package daos;

import models.Movie;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MovieDTO implements DTO<Movie> {
public int getId(Movie movie) {
return movie.getId();
}

public static Integer getIdOfLast(){
Connection conn = ConnectionFactory.getConnection();

try{
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT id FROM Movies ORDER BY id DESC LIMIT 1;");
if(rs.next()) return rs.getInt("id");
}catch (SQLException ex){
ex.printStackTrace();
}
return null;
}
}
Empty file removed src/main/java/models/DELETEME.txt
Empty file.
72 changes: 72 additions & 0 deletions src/main/java/models/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package models;

public class Movie {
Integer id;
String title;
Integer runtime;
Integer imdbscore;
Integer releaseyear;

public Movie(String title, Integer runtime, Integer imdbscore, Integer releaseyear){
this.title = title;
this.runtime = runtime;
this.imdbscore = imdbscore;
this.releaseyear = releaseyear;
}

public Movie(Integer id, String title, Integer runtime, Integer imdbscore, Integer releaseyear){
this.id = id;
this.title = title;
this.runtime = runtime;
this.imdbscore = imdbscore;
this.releaseyear = releaseyear;
}

public Movie(){}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Integer getRuntime() {
return runtime;
}

public void setRuntime(Integer runtime) {
this.runtime = runtime;
}

public Integer getImdbscore() {
return imdbscore;
}

public void setImdbscore(Integer imdbscore) {
this.imdbscore = imdbscore;
}

public Integer getReleaseyear() {
return releaseyear;
}

public void setReleaseyear(Integer releaseyear) {
this.releaseyear = releaseyear;
}

@Override
public String toString(){
return String.format("%s | %s | %s | %s",
this.getTitle(), this.getRuntime(), this.getImdbscore(), this.getReleaseyear());
}
}
Empty file removed src/test/java/daos/DELETEME.txt
Empty file.
Loading