Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RasberryPi;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JOptionPane;
- import com.mysql.jdbc.PreparedStatement;
- public class DatabaseManager {
- public Connection connect = null;
- public Statement stmt = null;
- public String sPathServerFile = "/Users/lukasstrunk/Documents/Ausbildung/OvMPrivatMac/Lernfeld-06 Anwendungsentwicklung/3.Ausbildungsjahr/Raspi RFID/Server.dat";
- String status = "";
- // ctor
- public DatabaseManager() {
- // database initials
- ArrayList<String> alServerInfos = ReadServerDataFromFile();
- // Aufbau der Datenbankverbindung
- String server = alServerInfos.get(0);// "192.168.1.101";
- String database = alServerInfos.get(1);// "karpfen";
- String uid = alServerInfos.get(2);// "karpfen";
- String passwd = alServerInfos.get(3); // "tollefische";
- String jdbc_driver = alServerInfos.get(4);// "com.mysql.jdbc.Driver";
- String connectionString = "jdbc:mysql://" + server + "/" + database + "?user" + uid + "&password=" + passwd
- + "autoReconnect=true&useSSL=false";
- // Build up Database Connectivity
- try {
- Class.forName(jdbc_driver);
- connect = DriverManager.getConnection(connectionString, uid, passwd);
- if (connect == null) {
- connect = DriverManager.getConnection(connectionString, uid, passwd);
- }
- } catch (ClassNotFoundException e) {
- System.out.println("Class could not been found!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Connection to Database could not been initialized");
- e.printStackTrace();
- }
- status = GetStatusString();
- }
- private String GetStatusString() {
- String sql = "SELECT * FROM Status";
- try {
- stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
- ResultSet rs = stmt.executeQuery(sql);
- if (rs == null) {
- PreparedStatement prepstmt = (PreparedStatement) connect.prepareStatement(sql);
- rs = stmt.executeQuery(sql);
- }
- // Lesen eines Datensätzen
- while (rs.next()) {
- status = rs.getString("status");
- }
- if (status.length() > 0) {
- return status;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- // Functions
- private ArrayList<String> ReadServerDataFromFile() {
- String line = "";
- ArrayList<String> alServerInfos = new ArrayList();
- int indexOfEquals = -1;
- File fServerInfos = new File(sPathServerFile);
- FileReader filereader;
- try {
- filereader = new FileReader(fServerInfos);
- BufferedReader br = new BufferedReader(filereader);
- if (fServerInfos.exists()) {
- while ((line = br.readLine()) != null) {
- String serverdata = "";
- String token = "";
- if (line.contains("=")) {
- indexOfEquals = line.indexOf("=");
- }
- for (int i = indexOfEquals + 1; i < line.length(); i++) {
- token += line.charAt(i);
- }
- serverdata = token;
- alServerInfos.add(serverdata);
- }
- return alServerInfos;
- } else {
- JOptionPane.showMessageDialog(null, "Server.dat ist nicht vorhanden");
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement