Skip to content
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

[GFTCodeFixer]: Update #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 12 additions & 15 deletions src/main/java/com/scalesec/vulnado/Comment.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.scalesec.vulnado;

import org.apache.catalina.Server;
import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.UUID;

public class Comment {
public String id, username, body;
public Timestamp created_on;
private String id, username, body;
private Timestamp createdOn;

public Comment(String id, String username, String body, Timestamp created_on) {
public Comment(String id, String username, String body, Timestamp createdOn) {
this.id = id;
this.username = username;
this.body = body;
this.created_on = created_on;
this.createdOn = createdOn;
}

public static Comment create(String username, String body){
Expand All @@ -33,50 +32,48 @@ public static Comment create(String username, String body){
}
}

public static List<Comment> fetch_all() {
public static List<Comment> fetchAll() {
Statement stmt = null;
List<Comment> comments = new ArrayList();
List<Comment> comments = new ArrayList<Comment>();
try {
Connection cxn = Postgres.connection();
stmt = cxn.createStatement();
try (Statement stmt = cxn.createStatement()) {

String query = "select * from comments;";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String id = rs.getString("id");
String username = rs.getString("username");
String body = rs.getString("body");
Timestamp created_on = rs.getTimestamp("created_on");
Comment c = new Comment(id, username, body, created_on);
Timestamp createdOn = rs.getTimestamp("created_on");
Comment c = new Comment(id, username, body, createdOn);
comments.add(c);
}
cxn.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
} finally {
return comments;
}
}

public static Boolean delete(String id) {
public static boolean delete(String id) {
try {
String sql = "DELETE FROM comments where id = ?";
Connection con = Postgres.connection();
PreparedStatement pStatement = con.prepareStatement(sql);
try (PreparedStatement pStatement = con.prepareStatement(sql)) {
pStatement.setString(1, id);
return 1 == pStatement.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
} finally {
return false;
}
}

private Boolean commit() throws SQLException {
String sql = "INSERT INTO comments (id, username, body, created_on) VALUES (?,?,?,?)";
Connection con = Postgres.connection();
PreparedStatement pStatement = con.prepareStatement(sql);
try (PreparedStatement pStatement = con.prepareStatement(sql)) {
pStatement.setString(1, this.id);
pStatement.setString(2, this.username);
pStatement.setString(3, this.body);
Expand Down