Files
GSE2TaskTracker/build/jacocoHtml/hhn.temp.project.provider/MySql.java.html
Jan-Philipp Luithardt 7f96f50fb7
Some checks failed
Gradle CI with Full Reports / build (push) Failing after 15m12s
mysql tests
2025-12-18 09:17:23 +01:00

223 lines
13 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="de"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MySql.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">GSE2TaskTracker</a> &gt; <a href="index.source.html" class="el_package">hhn.temp.project.provider</a> &gt; <span class="el_source">MySql.java</span></div><h1>MySql.java</h1><pre class="source lang-java linenums">package hhn.temp.project.provider;
import hhn.temp.project.Task;
import hhn.temp.project.TaskStatus;
import hhn.temp.project.expections.SQLNoConectionException;
import hhn.temp.project.expections.SQLStatmentException;
import hhn.temp.project.expections.TaskNotExistsException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MySql {
private String user;
private String password;
private String db;
private String host;
private String port;
private Connection connection;
<span class="fc" id="L22"> public MySql(String user, String password, String db, String host, String port) {</span>
<span class="fc" id="L23"> this.user = user;</span>
<span class="fc" id="L24"> this.password = password;</span>
<span class="fc" id="L25"> this.db = db;</span>
<span class="fc" id="L26"> this.host = host;</span>
<span class="fc" id="L27"> this.port = port;</span>
<span class="fc" id="L28"> }</span>
public void connect() {
<span class="fc" id="L31"> String url = &quot;jdbc:mysql://&quot; + host + &quot;:&quot; + port + &quot;/&quot; + db + &quot;?autoReconnect=true&amp;useSSL=false&quot;;</span>
try {
<span class="fc" id="L34"> Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);</span>
<span class="fc" id="L35"> connection = DriverManager.getConnection(url, user, password);</span>
<span class="fc" id="L36"> } catch (SQLException e) {</span>
<span class="fc" id="L37"> throw new SQLNoConectionException(&quot;Cant connect to the database&quot;, e);</span>
<span class="nc" id="L38"> } catch (ClassNotFoundException e) {</span>
<span class="nc" id="L39"> throw new SQLNoConectionException(&quot;Cant connect to the database: Driver class not found!&quot;, e);</span>
<span class="fc" id="L40"> }</span>
<span class="fc" id="L41"> }</span>
public void reset() {
<span class="fc bfc" id="L44" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L45"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L48"> PreparedStatement stmt = connection.prepareStatement(&quot;TRUNCATE task&quot;);</span>
<span class="fc" id="L49"> stmt.executeUpdate();</span>
<span class="fc" id="L50"> } catch (SQLException e) {</span>
<span class="fc" id="L51"> throw new SQLStatmentException(&quot;Cant insert data in database&quot;);</span>
<span class="fc" id="L52"> }</span>
<span class="fc" id="L53"> }</span>
public int createTask(String name, String description) {
<span class="fc bfc" id="L56" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L57"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L60"> PreparedStatement stmt = connection.prepareStatement(&quot;INSERT INTO task ( taskName, taskDescription) VALUES ( ? , ? )&quot;, Statement.RETURN_GENERATED_KEYS);</span>
<span class="fc" id="L61"> stmt.setString(1, name);</span>
<span class="fc" id="L62"> stmt.setString(2, description);</span>
<span class="fc" id="L63"> stmt.executeUpdate();</span>
<span class="fc" id="L64"> ResultSet rs = stmt.getGeneratedKeys();</span>
<span class="pc bpc" id="L65" title="1 of 2 branches missed."> if(rs.next()) {</span>
<span class="fc" id="L66"> return rs.getInt(1);</span>
}else{
<span class="nc" id="L68"> throw new SQLStatmentException(&quot;Can insert data but not select&quot;);</span>
}
<span class="fc" id="L70"> } catch (SQLException e) {</span>
<span class="fc" id="L71"> throw new SQLStatmentException(&quot;Cant insert data in database&quot;);</span>
}
}
public boolean existTask(String name) {
<span class="fc bfc" id="L77" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L78"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L81"> PreparedStatement stmt = connection.prepareStatement(&quot;Select * FROM task WHERE taskName=?&quot;);</span>
<span class="fc" id="L82"> stmt.setString(1, name);</span>
<span class="fc" id="L83"> ResultSet rs = stmt.executeQuery();</span>
<span class="fc" id="L84"> return rs.next();</span>
<span class="fc" id="L85"> } catch (SQLException e) {</span>
<span class="fc" id="L86"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
public boolean existTask(int id) {
<span class="fc bfc" id="L91" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L92"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L95"> PreparedStatement stmt = connection.prepareStatement(&quot;Select * FROM task WHERE taskID=?&quot;);</span>
<span class="fc" id="L96"> stmt.setString(1, &quot;&quot;+id);</span>
<span class="fc" id="L97"> ResultSet rs = stmt.executeQuery();</span>
<span class="fc" id="L98"> return rs.next();</span>
<span class="fc" id="L99"> } catch (SQLException e) {</span>
<span class="fc" id="L100"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
public void deleteTask(String name) {
<span class="fc bfc" id="L105" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L106"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L109"> PreparedStatement stmt = connection.prepareStatement(&quot;DELETE FROM task WHERE taskName=?&quot;);</span>
<span class="fc" id="L110"> stmt.setString(1, name);</span>
<span class="fc" id="L111"> stmt.executeUpdate();</span>
<span class="fc" id="L112"> } catch (SQLException e) {</span>
<span class="fc" id="L113"> throw new SQLStatmentException(&quot;Cant delete data in database&quot;);</span>
<span class="fc" id="L114"> }</span>
<span class="fc" id="L115"> }</span>
public void updateDescription(int id, String description) {
<span class="fc bfc" id="L118" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L119"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L122"> PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskDescription=? WHERE taskID=?&quot;);</span>
<span class="fc" id="L123"> stmt.setString(1, description);</span>
<span class="fc" id="L124"> stmt.setString(2, &quot;&quot;+id);</span>
<span class="fc" id="L125"> stmt.executeUpdate();</span>
<span class="fc" id="L127"> } catch (SQLException e) {</span>
<span class="fc" id="L128"> throw new SQLStatmentException(&quot;Cant update data in database&quot;);</span>
<span class="fc" id="L129"> }</span>
<span class="fc" id="L130"> }</span>
public void updateStatus(int id, TaskStatus status) {
<span class="fc bfc" id="L133" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L134"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L137"> PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskStatus=? WHERE taskID=?&quot;);</span>
<span class="fc" id="L138"> stmt.setString(1, status.name());</span>
<span class="fc" id="L139"> stmt.setString(2, &quot;&quot;+id);</span>
<span class="fc" id="L140"> stmt.executeUpdate();</span>
<span class="fc" id="L142"> } catch (SQLException e) {</span>
<span class="fc" id="L143"> throw new SQLStatmentException(&quot;Cant update data in database&quot;);</span>
<span class="fc" id="L144"> }</span>
<span class="fc" id="L145"> }</span>
public void updateWorker(int id, String worker) {
<span class="fc bfc" id="L149" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L150"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L153"> PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskWorker=? WHERE taskID=?&quot;);</span>
<span class="fc" id="L154"> stmt.setString(1, worker);</span>
<span class="fc" id="L155"> stmt.setString(2, &quot;&quot;+id);</span>
<span class="fc" id="L156"> stmt.executeUpdate();</span>
<span class="fc" id="L158"> } catch (SQLException e) {</span>
<span class="fc" id="L159"> throw new SQLStatmentException(&quot;Cant update data in database&quot;);</span>
<span class="fc" id="L160"> }</span>
<span class="fc" id="L161"> }</span>
public Task getTask(int id) {
<span class="fc bfc" id="L166" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L167"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L170"> PreparedStatement stmt = connection.prepareStatement(&quot;SELECT * FROM task WHERE taskID=?&quot;);</span>
<span class="fc" id="L171"> stmt.setString(1, &quot;&quot;+id);</span>
<span class="fc" id="L172"> ResultSet rs = stmt.executeQuery();</span>
<span class="fc bfc" id="L173" title="All 2 branches covered."> if(rs.next()) {</span>
<span class="fc" id="L174"> return getTaskFromDatabase(rs);</span>
} else {
<span class="fc" id="L176"> throw new TaskNotExistsException(&quot;No Task found in databse with id: &quot; + id);</span>
}
<span class="fc" id="L178"> } catch (SQLException e) {</span>
<span class="fc" id="L179"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
public Task getTask(String name) {
<span class="fc bfc" id="L183" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L184"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L187"> PreparedStatement stmt = connection.prepareStatement(&quot;SELECT * FROM task WHERE taskName=?&quot;);</span>
<span class="fc" id="L188"> stmt.setString(1, name);</span>
<span class="fc" id="L189"> ResultSet rs = stmt.executeQuery();</span>
<span class="fc bfc" id="L190" title="All 2 branches covered."> if(rs.next()) {</span>
<span class="fc" id="L191"> return getTaskFromDatabase(rs);</span>
}else{
<span class="fc" id="L193"> throw new TaskNotExistsException(&quot;No Task found in databse with name: &quot; + name);</span>
}
<span class="fc" id="L196"> } catch (SQLException e) {</span>
<span class="fc" id="L197"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
public List&lt;Task&gt; getTaskList() {
<span class="fc bfc" id="L202" title="All 2 branches covered."> if(connection == null) {</span>
<span class="fc" id="L203"> throw new NullPointerException(&quot;You must first connect to the Database.&quot;);</span>
}
try {
<span class="fc" id="L206"> PreparedStatement stmt = connection.prepareStatement(&quot;SELECT * FROM task&quot;);</span>
<span class="fc" id="L207"> ResultSet rs = stmt.executeQuery();</span>
<span class="fc" id="L208"> List&lt;Task&gt; taskList = new ArrayList&lt;&gt;();</span>
<span class="fc bfc" id="L209" title="All 2 branches covered."> while(rs.next()) {</span>
<span class="fc" id="L210"> taskList.add(getTaskFromDatabase(rs));</span>
}
<span class="fc" id="L212"> return taskList;</span>
<span class="fc" id="L213"> } catch (SQLException e) {</span>
<span class="fc" id="L214"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
private Task getTaskFromDatabase(ResultSet rs) throws SQLException {
<span class="fc" id="L219"> Task task = new Task(rs.getInt(&quot;taskID&quot;), rs.getString(&quot;taskName&quot;), rs.getString(&quot;taskDescription&quot;), rs.getString(&quot;taskWorker&quot;),TaskStatus.valueOf(rs.getString(&quot;taskStatus&quot;)), this);</span>
<span class="fc" id="L220"> return task;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.13.202504020838</span></div></body></html>