Guest User

Untitled

a guest
Feb 12th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.text.DateFormat;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.Properties;
  10.  
  11. import com.pi4j.io.gpio.PinState;
  12.  
  13. import java.sql.*;
  14.  
  15.  
  16. public class Baza_danych
  17. {
  18.  
  19.  
  20. static String daneZBazy;
  21.  
  22.  
  23. public static void Baza_danych2(int id_sensor, String data_measure, String data_time,String data_calibration, String sector )
  24. {
  25.  
  26.  
  27.  
  28.  
  29. Properties props = new Properties();
  30. props.setProperty("user","psmorczewski");
  31. props.setProperty("password","3uSMk2Yu2v4pbgwL");
  32. props.setProperty("useSSL","false");
  33. String polaczenieURL = "jdbc:mysql://157.158.81.79:33306/psmorczewski";
  34.  
  35.  
  36. try
  37. { DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  38. Date time_ = new Date();
  39. Class.forName("com.mysql.jdbc.Driver");
  40. Connection connect=DriverManager.getConnection(polaczenieURL,props);
  41. Statement stmt=connect.createStatement();
  42. System.out.println("Prawidlowo połączylem sie z Bazą danych psmorczewski");
  43. /*String sql = "CREATE TABLE SENSOR_MEASUREMENT " +
  44. " (id_sensor VARCHAR(255), " +
  45. " data_time VARCHAR(255), "+
  46. " sector_sensor VARCHAR(255), " +
  47. " data_measure VARCHAR(255), " +
  48. " data_calibration VARCHAR(255)) " ;
  49.  
  50. stmt.executeUpdate(sql);
  51. System.out.println("Created table in given database...");
  52. */
  53.  
  54. stmt.executeUpdate("INSERT INTO SENSOR_MEASUREMENT(id_sensor,data_measure,data_time,data_calibration,sector_sensor) VALUES ('"+id_sensor+"', '"+data_measure+"', '"+data_time+"','"+data_calibration+"','"+sector+"')");
  55. connect.close();
  56.  
  57. }
  58.  
  59. catch(ClassNotFoundException wyjatek)
  60. {
  61. System.out.println("Database driver problem");
  62.  
  63. }
  64.  
  65. catch(SQLException wyjatek)
  66. {
  67. //e.printStackTrace();
  68. DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  69. Date time_begin = new Date();
  70.  
  71. System.out.println("Problem with login\n PLEASE CHECK:\n user name, password, database name, IP adress");
  72. //System.out.println("SQLException: " + wyjatek.getMessage());
  73. //System.out.println("SQLState: " + wyjatek.getSQLState());
  74. System.out.println("VendorError: " + wyjatek.getErrorCode());
  75.  
  76. }
  77.  
  78. }
  79.  
  80.  
  81. public static int Connecting() throws ClassNotFoundException
  82. { Connection con;
  83. int verification=1;
  84. Properties props = new Properties();
  85. props.setProperty("user","psmorczewski");
  86. props.setProperty("password","3uSMk2Yu2v4pbgwL");
  87. props.setProperty("useSSL","false");
  88.  
  89.  
  90. try {
  91. String polaczenieURL = "jdbc:mysql://157.158.81.79:33306/psmorczewski";
  92. Class.forName("com.mysql.jdbc.Driver");
  93. con = DriverManager.getConnection(polaczenieURL,props);
  94. System.out.println("Connected to database psmorczewski");
  95. verification=1;
  96.  
  97. }
  98. catch (SQLException e)
  99. {
  100. System.out.println("No connection to base");
  101. verification=0;
  102. //e.printStackTrace();
  103. }
  104.  
  105.  
  106. return verification;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. static void wyswietlDaneZBazy(ResultSet rs)
  113. {
  114. try
  115. {
  116. daneZBazy = rs.getString(1);
  117. System.out.println("\n" + daneZBazy + " ");
  118. daneZBazy = rs.getString(2);
  119. System.out.println(daneZBazy + " ");
  120. daneZBazy = rs.getString(3);
  121. System.out.println(daneZBazy);
  122. }
  123. catch(SQLException e)
  124. {
  125. e.printStackTrace();
  126. }
  127. }
  128.  
  129. }
Add Comment
Please, Sign In to add comment