Skip to content
Snippets Groups Projects
Commit 6c41fa49 authored by Puissegur Alexis's avatar Puissegur Alexis
Browse files

adding sqlitedb + table + insert methods

parent c40d27fe
No related branches found
No related tags found
No related merge requests found
......@@ -31,6 +31,7 @@ endorsed.classpath=
excludes=
file.reference.bluecove-2.1.1-SNAPSHOT.jar=bluecove-2.1.1-SNAPSHOT.jar
file.reference.javax.mail-api-1.4.7.jar=javax.mail-api-1.4.7.jar
file.reference.sqlite-jdbc-3.23.1.jar=C:\\Users\\puiss\\Downloads\\sqlite-jdbc-3.23.1.jar
includes=**
# Non-JavaFX jar file creation is deactivated in JavaFX 2.0+ projects
jar.archive.disabled=true
......@@ -38,7 +39,8 @@ jar.compress=false
javac.classpath=\
${javafx.classpath.extension}:\
${file.reference.bluecove-2.1.1-SNAPSHOT.jar}:\
${file.reference.javax.mail-api-1.4.7.jar}
${file.reference.javax.mail-api-1.4.7.jar}:\
${file.reference.sqlite-jdbc-3.23.1.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
......
......@@ -40,6 +40,12 @@ import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
* FXML Controller class
*
......@@ -196,9 +202,85 @@ public class FXMLVueController implements Initializable, Observer {
graph.getData().add(serieLine);
graph.setLegendVisible(false);
initDB("plant.db");
}
public static void initDB(String name) {
String url = "jdbc:sqlite:C:/sqlite/" + name;
try (Connection conn = DriverManager.getConnection(url)) {
if (conn != null) {
DatabaseMetaData meta = conn.getMetaData();
createNewTable(url);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Create a new table in the test database
*
*/
public static void createNewTable(String url) {
// SQLite connection string
// SQL statement for creating a new table
String sql = "CREATE TABLE IF NOT EXISTS data (\n"
+ " id integer PRIMARY KEY AUTOINCREMENT,\n"
+ " month integer NOT NULL CHECK(month <= 12 and month > 0),\n"
+ " day integer NOT NULL CHECK(day <= 31 and day > 0),\n"
+ " hour integer NOT NULL CHECK(hour <24 and hour >= 0),\n"
+ " min integer NOT NULL CHECK(min <60 and min >= 0),\n"
+ " intensity integer,\n"
+ " temperature integer,\n"
+ " humidityAir integer,\n"
+ " humidityPlant integer\n"
+ ");";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
// create a new table
stmt.execute(sql);
} catch (SQLException e) {
System.out.println("ewi" + e.getMessage());
}
}
private Connection connect() {
// SQLite connection string
String url = "jdbc:sqlite:C://sqlite/plant.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public void insert(int month, int day, int hour, int min, int it, int temp, int humiA, int humiT) {
String sql = "INSERT INTO data(month,day,hour,min,intensity,temperature,humidityAir,humidityPlant) VALUES(?,?,?,?,?,?,?,?)";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, month);
pstmt.setInt(2, day);
pstmt.setInt(3, hour);
pstmt.setInt(4, min);
pstmt.setInt(5, it);
pstmt.setInt(6, temp);
pstmt.setInt(7, humiA);
pstmt.setInt(8, humiT);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Change the text for activate or desactivate according to the checkbox
* @param event
......
......@@ -14,6 +14,7 @@ import Vue.*;
import java.io.IOException;
import javafx.scene.image.Image;
/**
*
* @author invite
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment