Files
GSE2TaskTracker/build/jacocoHtml/hhn.temp.project.provider/MySql.java.html
Jan-Philipp Luithardt c775b4ab96
Some checks are pending
Gradle CI with Jacoco / build (push) Has started running
add db
2025-12-06 20:08:03 +01:00

227 lines
12 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 Connection connection;
<span class="fc" id="L21"> public MySql(String user, String password, String db, String host) {</span>
<span class="fc" id="L22"> this.user = user;</span>
<span class="fc" id="L23"> this.password = password;</span>
<span class="fc" id="L24"> this.db = db;</span>
<span class="fc" id="L25"> this.host = host;</span>
<span class="fc" id="L26"> }</span>
public void connect() {
<span class="fc" id="L29"> String url = &quot;jdbc:mysql://&quot; + host + &quot;:3306/&quot; + db + &quot;?autoReconnect=true&amp;useSSL=false&quot;;</span>
try {
<span class="fc" id="L32"> Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);</span>
<span class="fc" id="L33"> connection = DriverManager.getConnection(url, user, password);</span>
<span class="fc" id="L34"> } catch (SQLException e) {</span>
<span class="fc" id="L35"> throw new SQLNoConectionException(&quot;Cant connect to the database&quot;);</span>
<span class="nc" id="L36"> } catch (ClassNotFoundException e) {</span>
<span class="nc" id="L37"> throw new SQLNoConectionException(&quot;Cant connect to the database: Driver class not found!&quot;);</span>
<span class="fc" id="L38"> }</span>
<span class="fc" id="L39"> }</span>
public void reset() {
try {
<span class="fc" id="L43"> PreparedStatement stmt = connection.prepareStatement(&quot;TRUNCATE task&quot;);</span>
<span class="fc" id="L44"> stmt.executeUpdate();</span>
<span class="nc" id="L45"> } catch (SQLException e) {</span>
<span class="nc" id="L46"> throw new SQLStatmentException(&quot;Cant insert data in database&quot;);</span>
<span class="fc" id="L47"> }</span>
<span class="fc" id="L48"> }</span>
public int createTask(String name, String description) {
try {
<span class="fc" id="L52"> PreparedStatement stmt = connection.prepareStatement(&quot;INSERT INTO task ( taskName, taskDescription) VALUES ( ? , ? )&quot;, Statement.RETURN_GENERATED_KEYS);</span>
<span class="fc" id="L53"> stmt.setString(1, name);</span>
<span class="fc" id="L54"> stmt.setString(2, description);</span>
<span class="fc" id="L55"> stmt.executeUpdate();</span>
<span class="fc" id="L56"> ResultSet rs = stmt.getGeneratedKeys();</span>
<span class="pc bpc" id="L57" title="1 of 2 branches missed."> if(rs.next()) {</span>
<span class="fc" id="L58"> return rs.getInt(1);</span>
}else{
<span class="nc" id="L60"> throw new SQLStatmentException(&quot;Can insert data but not select&quot;);</span>
}
<span class="nc" id="L62"> } catch (SQLException e) {</span>
<span class="nc" id="L63"> throw new SQLStatmentException(&quot;Cant insert data in database&quot;);</span>
}
}
// public void deleteTask(int id) {
// try {
// PreparedStatement stmt = connection.prepareStatement(&quot;DELETE task WHERE taskID=?&quot;);
// stmt.setString(1, &quot;&quot;+id);
// stmt.executeUpdate();
// } catch (SQLException e) {
// throw new SQLStatmentException(&quot;Cant delete data in database&quot;);
// }
// }
public boolean existTask(String name) {
try {
<span class="fc" id="L79"> PreparedStatement stmt = connection.prepareStatement(&quot;Select * FROM task WHERE taskName=?&quot;);</span>
<span class="fc" id="L80"> stmt.setString(1, name);</span>
<span class="fc" id="L81"> ResultSet rs = stmt.executeQuery();</span>
<span class="fc" id="L82"> return rs.next();</span>
<span class="nc" id="L83"> } catch (SQLException e) {</span>
<span class="nc" id="L84"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
public boolean existTask(int id) {
try {
<span class="fc" id="L90"> PreparedStatement stmt = connection.prepareStatement(&quot;Select * FROM task WHERE taskID=?&quot;);</span>
<span class="fc" id="L91"> stmt.setString(1, &quot;&quot;+id);</span>
<span class="fc" id="L92"> ResultSet rs = stmt.executeQuery();</span>
<span class="fc" id="L93"> return rs.next();</span>
<span class="nc" id="L94"> } catch (SQLException e) {</span>
<span class="nc" id="L95"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
public void deleteTask(String name) {
try {
<span class="fc" id="L101"> PreparedStatement stmt = connection.prepareStatement(&quot;DELETE FROM task WHERE taskName=?&quot;);</span>
<span class="fc" id="L102"> stmt.setString(1, name);</span>
<span class="fc" id="L103"> stmt.executeUpdate();</span>
<span class="nc" id="L104"> } catch (SQLException e) {</span>
<span class="nc" id="L105"> throw new SQLStatmentException(&quot;Cant delete data in database&quot;);</span>
<span class="fc" id="L106"> }</span>
<span class="fc" id="L107"> }</span>
public void updateDescription(int id, String description) {
try {
<span class="fc" id="L111"> PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskDescription=? WHERE taskID=?&quot;);</span>
<span class="fc" id="L112"> stmt.setString(1, description);</span>
<span class="fc" id="L113"> stmt.setString(2, &quot;&quot;+id);</span>
<span class="fc" id="L114"> stmt.executeUpdate();</span>
<span class="nc" id="L116"> } catch (SQLException e) {</span>
<span class="nc" id="L117"> throw new SQLStatmentException(&quot;Cant update data in database&quot;);</span>
<span class="fc" id="L118"> }</span>
<span class="fc" id="L119"> }</span>
// public void updateDescription(String name, String description) {
// try {
// PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskDescription=? WHERE taskName=?&quot;);
// stmt.setString(1, description);
// stmt.setString(2, name);
// stmt.executeUpdate();
//
// } catch (SQLException e) {
// throw new SQLStatmentException(&quot;Cant update data in database&quot;);
// }
// }
public void updateStatus(int id, TaskStatus status) {
try {
<span class="fc" id="L133"> PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskStatus=? WHERE taskID=?&quot;);</span>
<span class="fc" id="L134"> stmt.setString(1, status.name());</span>
<span class="fc" id="L135"> stmt.setString(2, &quot;&quot;+id);</span>
<span class="fc" id="L136"> stmt.executeUpdate();</span>
<span class="nc" id="L138"> } catch (SQLException e) {</span>
<span class="nc" id="L139"> throw new SQLStatmentException(&quot;Cant update data in database&quot;);</span>
<span class="fc" id="L140"> }</span>
<span class="fc" id="L141"> }</span>
// public void updateStatus(String name, TaskStatus status) {
// try {
// PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskStatus=? WHERE taskName=?&quot;);
// stmt.setString(1, status.name());
// stmt.setString(2, name);
// stmt.executeUpdate();
//
// } catch (SQLException e) {
// throw new SQLStatmentException(&quot;Cant update data in database&quot;);
// }
// }
public void updateWorker(int id, String worker) {
try {
<span class="fc" id="L156"> PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskWorker=? WHERE taskID=?&quot;);</span>
<span class="fc" id="L157"> stmt.setString(1, worker);</span>
<span class="fc" id="L158"> stmt.setString(2, &quot;&quot;+id);</span>
<span class="fc" id="L159"> stmt.executeUpdate();</span>
<span class="nc" id="L161"> } catch (SQLException e) {</span>
<span class="nc" id="L162"> throw new SQLStatmentException(&quot;Cant update data in database&quot;);</span>
<span class="fc" id="L163"> }</span>
<span class="fc" id="L164"> }</span>
// public void updateWorker(String name, String worker) {
// try {
// PreparedStatement stmt = connection.prepareStatement(&quot;UPDATE task SET taskWorker=? WHERE taskName=?&quot;);
// stmt.setString(1, worker);
// stmt.setString(2, name);
// stmt.executeUpdate();
//
// } catch (SQLException e) {
// throw new SQLStatmentException(&quot;Cant update data in database&quot;);
// }
// }
public Task getTask(int id) {
try {
<span class="fc" id="L180"> PreparedStatement stmt = connection.prepareStatement(&quot;SELECT * FROM task WHERE taskID=?&quot;);</span>
<span class="fc" id="L181"> stmt.setString(1, &quot;&quot;+id);</span>
<span class="fc" id="L182"> ResultSet rs = stmt.executeQuery();</span>
<span class="pc bpc" id="L183" title="1 of 2 branches missed."> if(rs.next()) {</span>
<span class="fc" id="L184"> return getTaskFromDatabase(rs);</span>
} else {
<span class="nc" id="L186"> throw new TaskNotExistsException(&quot;No Task found in databse with id: &quot; + id);</span>
}
<span class="nc" id="L188"> } catch (SQLException e) {</span>
<span class="nc" id="L189"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
public Task getTask(String name) {
try {
<span class="fc" id="L194"> PreparedStatement stmt = connection.prepareStatement(&quot;SELECT * FROM task WHERE taskName=?&quot;);</span>
<span class="fc" id="L195"> stmt.setString(1, name);</span>
<span class="fc" id="L196"> ResultSet rs = stmt.executeQuery();</span>
<span class="pc bpc" id="L197" title="1 of 2 branches missed."> if(rs.next()) {</span>
<span class="fc" id="L198"> return getTaskFromDatabase(rs);</span>
}else{
<span class="nc" id="L200"> throw new TaskNotExistsException(&quot;No Task found in databse with name: &quot; + name);</span>
}
<span class="nc" id="L203"> } catch (SQLException e) {</span>
<span class="nc" id="L204"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
public List&lt;Task&gt; getTaskList() {
try {
<span class="fc" id="L210"> PreparedStatement stmt = connection.prepareStatement(&quot;SELECT * FROM task&quot;);</span>
<span class="fc" id="L211"> ResultSet rs = stmt.executeQuery();</span>
<span class="fc" id="L212"> List&lt;Task&gt; taskList = new ArrayList&lt;&gt;();</span>
<span class="fc bfc" id="L213" title="All 2 branches covered."> while(rs.next()) {</span>
<span class="fc" id="L214"> taskList.add(getTaskFromDatabase(rs));</span>
}
<span class="fc" id="L216"> return taskList;</span>
<span class="nc" id="L217"> } catch (SQLException e) {</span>
<span class="nc" id="L218"> throw new SQLStatmentException(&quot;Cant select data from database&quot;);</span>
}
}
private Task getTaskFromDatabase(ResultSet rs) throws SQLException {
<span class="fc" id="L223"> 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="L224"> 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>