Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. package RasberryPi;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.Timer;
  15. import java.util.TimerTask;
  16.  
  17. import javax.swing.JOptionPane;
  18.  
  19. import com.mysql.jdbc.PreparedStatement;
  20.  
  21. public class DatabaseManager {
  22.  
  23.     public Connection connect = null;
  24.     public Statement stmt = null;
  25.  
  26.     public String sPathServerFile = "/Users/lukasstrunk/Documents/Ausbildung/OvMPrivatMac/Lernfeld-06 Anwendungsentwicklung/3.Ausbildungsjahr/Raspi RFID/Server.dat";
  27.     String status = "";
  28.  
  29.     // ctor
  30.     public DatabaseManager() {
  31.  
  32.         // database initials
  33.         ArrayList<String> alServerInfos = ReadServerDataFromFile();
  34.  
  35.         // Aufbau der Datenbankverbindung
  36.         String server = alServerInfos.get(0);// "192.168.1.101";
  37.         String database = alServerInfos.get(1);// "karpfen";
  38.         String uid = alServerInfos.get(2);// "karpfen";
  39.         String passwd = alServerInfos.get(3); // "tollefische";
  40.         String jdbc_driver = alServerInfos.get(4);// "com.mysql.jdbc.Driver";
  41.  
  42.         String connectionString = "jdbc:mysql://" + server + "/" + database + "?user" + uid + "&password=" + passwd
  43.                 + "autoReconnect=true&useSSL=false";
  44.  
  45.         // Build up Database Connectivity
  46.         try {
  47.             Class.forName(jdbc_driver);
  48.  
  49.             connect = DriverManager.getConnection(connectionString, uid, passwd);
  50.  
  51.             if (connect == null) {
  52.                 connect = DriverManager.getConnection(connectionString, uid, passwd);
  53.             }
  54.  
  55.         } catch (ClassNotFoundException e) {
  56.             System.out.println("Class could not been found!");
  57.             e.printStackTrace();
  58.  
  59.         } catch (SQLException e) {
  60.             System.out.println("Connection to Database could not been initialized");
  61.             e.printStackTrace();
  62.         }
  63.         status = GetStatusString();
  64.     }
  65.  
  66.     private String GetStatusString() {
  67.  
  68.         String sql = "SELECT * FROM Status";
  69.         try {
  70.             stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  71.  
  72.             ResultSet rs = stmt.executeQuery(sql);
  73.  
  74.             if (rs == null) {
  75.                 PreparedStatement prepstmt = (PreparedStatement) connect.prepareStatement(sql);
  76.                 rs = stmt.executeQuery(sql);
  77.             }
  78.             // Lesen eines Datensätzen
  79.             while (rs.next()) {
  80.                 status = rs.getString("status");
  81.             }
  82.             if (status.length() > 0) {
  83.                 return status;
  84.             }
  85.         } catch (SQLException e) {
  86.             // TODO Auto-generated catch block
  87.             e.printStackTrace();
  88.         }
  89.         return null;
  90.     }
  91.  
  92.     // Functions
  93.     private ArrayList<String> ReadServerDataFromFile() {
  94.         String line = "";
  95.         ArrayList<String> alServerInfos = new ArrayList();
  96.         int indexOfEquals = -1;
  97.         File fServerInfos = new File(sPathServerFile);
  98.         FileReader filereader;
  99.         try {
  100.             filereader = new FileReader(fServerInfos);
  101.             BufferedReader br = new BufferedReader(filereader);
  102.  
  103.             if (fServerInfos.exists()) {
  104.  
  105.                 while ((line = br.readLine()) != null) {
  106.                     String serverdata = "";
  107.                     String token = "";
  108.                     if (line.contains("=")) {
  109.                         indexOfEquals = line.indexOf("=");
  110.                     }
  111.                     for (int i = indexOfEquals + 1; i < line.length(); i++) {
  112.  
  113.                         token += line.charAt(i);
  114.                     }
  115.                     serverdata = token;
  116.                     alServerInfos.add(serverdata);
  117.  
  118.                 }
  119.                 return alServerInfos;
  120.             } else {
  121.                 JOptionPane.showMessageDialog(null, "Server.dat ist nicht vorhanden");
  122.             }
  123.  
  124.         } catch (FileNotFoundException e) {
  125.             e.printStackTrace();
  126.         } catch (IOException e) {
  127.             // TODO Auto-generated catch block
  128.             e.printStackTrace();
  129.         }
  130.         return null;
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement