implementd basic Question Service
This commit is contained in:
parent
2652138ddd
commit
cb18e47557
@ -1,14 +1,27 @@
|
||||
package at.ionas999.questioncatalog.controller;
|
||||
|
||||
import at.ionas999.questioncatalog.model.Question;
|
||||
import at.ionas999.questioncatalog.services.QuestionService;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.TextField;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class ViewController{
|
||||
public Button editButton;
|
||||
public Button deleteButton;
|
||||
public TextField answerField;
|
||||
public ListView questionListView;
|
||||
public TextField questionField;
|
||||
|
||||
|
||||
@FXML
|
||||
private void initialize() throws SQLException {
|
||||
List<Question> questions = QuestionService.GetQuestionsFromCatalog(1);
|
||||
System.out.println(questions);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
package at.ionas999.questioncatalog.model;
|
||||
|
||||
public class Question {
|
||||
private int id;
|
||||
private String question;
|
||||
private String answer;
|
||||
private int questionCatalogId;
|
||||
|
||||
public Question(int id,String question, String answer, int questionCatalogId) {
|
||||
this.id = id;
|
||||
this.question = question;
|
||||
this.answer = answer;
|
||||
this.questionCatalogId = questionCatalogId;
|
||||
}
|
||||
|
||||
public int getQuestionCatalogId()
|
||||
{
|
||||
return questionCatalogId;
|
||||
}
|
||||
public String getQuestion() {
|
||||
return question;
|
||||
}
|
||||
|
||||
public void setQuestion(String question) {
|
||||
this.question = question;
|
||||
}
|
||||
|
||||
public String getAnswer() {
|
||||
return answer;
|
||||
}
|
||||
|
||||
public void setAnswer(String answer) {
|
||||
this.answer = answer;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package at.ionas999.questioncatalog.model;
|
||||
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class QuestionCatalog {
|
||||
private int id;
|
||||
private ArrayList<Question> questions;
|
||||
private String name;
|
||||
|
||||
public QuestionCatalog(String name, ArrayList<Question> questions) {
|
||||
this.name = name;
|
||||
this.questions = questions;
|
||||
}
|
||||
|
||||
public ObservableList<Question> getQuestions() {
|
||||
return FXCollections.observableList(questions);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
package at.ionas999.questioncatalog.services;
|
||||
|
||||
import at.ionas999.questioncatalog.model.Question;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class QuestionService {
|
||||
public static boolean AddQuestionToDb(Question question)
|
||||
{
|
||||
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
||||
String insertStmt = "insert into QUESTION (QUESTIONCATALOGID, QUESTION, ANSWER) VALUES (?, ?, ?)";
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement(insertStmt)){
|
||||
statement.setInt(1, question.getQuestionCatalogId());
|
||||
statement.setString(2, question.getQuestion());
|
||||
statement.setString(3, question.getAnswer());
|
||||
statement.execute();
|
||||
}
|
||||
}
|
||||
catch (SQLException _){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static List<Question> GetQuestionsFromCatalog(int catalogId) throws SQLException {
|
||||
String stmtString = "select id, questionCatalogId, question, answer from Question where questionCatalogId = ?";
|
||||
|
||||
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement(stmtString)){
|
||||
statement.setInt(1, catalogId);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
List<Question> questions = new ArrayList<Question>();
|
||||
|
||||
while (rs.next()){
|
||||
Question question = new Question(
|
||||
rs.getInt(1),
|
||||
rs.getString(3),
|
||||
rs.getString(4),
|
||||
rs.getInt(2)
|
||||
);
|
||||
questions.add(question);
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package at.ionas999.questioncatalog.services;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public abstract class ServiceBase {
|
||||
|
||||
public static Connection GetJDBCConnection() throws SQLException {
|
||||
Connection con = DriverManager.getConnection("jdbc:derby:/tmp/questionCatalog.db;create=true");
|
||||
CreateTables(con);
|
||||
return con;
|
||||
}
|
||||
|
||||
private static void CreateTables(Connection connection) throws SQLException {
|
||||
if (!doesTableExist(connection, "QUESTIONCATALOG")) {
|
||||
System.out.println("does x");
|
||||
String createQuestionCatalogTable = """
|
||||
create table QuestionCatalog
|
||||
(
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
|
||||
name varchar(10) not null
|
||||
)
|
||||
""";
|
||||
try (Statement stmt = connection.createStatement()) {
|
||||
stmt.execute(createQuestionCatalogTable);
|
||||
}
|
||||
}
|
||||
|
||||
if (!doesTableExist(connection, "QUESTION")) {
|
||||
System.out.println("does x");
|
||||
String createQuestionTable = """
|
||||
create table Question
|
||||
(
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
|
||||
questionCatalogId integer,
|
||||
question varchar(100) not null,
|
||||
answer varchar(100) not null,
|
||||
constraint fk_questionCatalog foreign key (questionCatalogId) references QuestionCatalog(id)
|
||||
)
|
||||
""";
|
||||
try (Statement stmt = connection.createStatement()) {
|
||||
stmt.execute(createQuestionTable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean doesTableExist(Connection connection, String tableName) throws SQLException {
|
||||
try (var resultSet = connection.getMetaData().getTables(null, null, tableName.toUpperCase(), null)) {
|
||||
return resultSet.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ module at.ionas999.questioncatalog {
|
||||
requires javafx.controls;
|
||||
requires javafx.fxml;
|
||||
requires java.desktop;
|
||||
requires java.sql;
|
||||
|
||||
opens at.ionas999.questioncatalog to javafx.fxml;
|
||||
opens at.ionas999.questioncatalog.controller to javafx.fxml; // Add this line
|
||||
|
||||
Loading…
Reference in New Issue
Block a user