Guest User

Untitled

a guest
Feb 11th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 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.util.Date;
  8. import java.util.Properties;
  9.  
  10. import com.pi4j.io.gpio.PinState;
  11.  
  12. import java.sql.*;
  13.  
  14.  
  15. public class Baza_danych
  16. {
  17.  
  18.  
  19. static String daneZBazy;
  20.  
  21.  
  22. public static void Baza_danych2(int id, String data_m, String data_t,String data_c, String sector )
  23. {
  24.  
  25. System.out.println("Stwarzam nowa tabele w bazie danych o nazwie czujnik");
  26.  
  27.  
  28. Properties props = new Properties();
  29. props.setProperty("user","psmorczewski");
  30. props.setProperty("password","3uSMk2Yu2v4pbgwL");
  31. props.setProperty("ssl","false");
  32. String polaczenieURL = "jdbc:mysql://157.158.81.79:33306/psmorczewski";
  33.  
  34.  
  35. try
  36. {
  37. Class.forName("com.mysql.jdbc.Driver");
  38. Connection connect=DriverManager.getConnection(polaczenieURL,props);
  39. Statement stmt=connect.createStatement();
  40. System.out.println("Prawidlowo polaczylem sie za baza danych");
  41. /*String sql = "CREATE TABLE SENSOR_MEASUREMENT " +
  42. " (id_sensor VARCHAR(255), " +
  43. " data_time VARCHAR(255), "+
  44. " sector_sensor VARCHAR(255), " +
  45. " data_measure VARCHAR(255), " +
  46. " data_calibration VARCHAR(255)) " ;
  47.  
  48. stmt.executeUpdate(sql);
  49. System.out.println("Created table in given database...");
  50. */
  51.  
  52. stmt.executeUpdate("INSERT INTO SENSOR_MEASUREMENT(id_sensor,data_measure,data_time,data_calibration,sector_sensor) VALUES ('"+id+"', '"+data_m+"', '"+data_t+"','"+data_c+"','"+sector+"')");
  53. connect.close();
  54.  
  55. }
  56.  
  57. catch(ClassNotFoundException wyjatek)
  58. {
  59. System.out.println("Problem ze sterownikiem");
  60.  
  61. }
  62.  
  63. catch(SQLException wyjatek)
  64. {
  65. //e.printStackTrace();
  66.  
  67. System.out.println("Problem z logowaniem\nProsze sprawdzic:\n nazwę użytkownika, hasło, nazwę bazy danych lub adres IP serwera");
  68. System.out.println("SQLException: " + wyjatek.getMessage());
  69. System.out.println("SQLState: " + wyjatek.getSQLState());
  70. System.out.println("VendorError: " + wyjatek.getErrorCode());
  71. }
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78. static void wyswietlDaneZBazy(ResultSet rs)
  79. {
  80. try
  81. {
  82. daneZBazy = rs.getString(1);
  83. System.out.println("\n" + daneZBazy + " ");
  84. daneZBazy = rs.getString(2);
  85. System.out.println(daneZBazy + " ");
  86. daneZBazy = rs.getString(3);
  87. System.out.println(daneZBazy);
  88. }
  89. catch(SQLException e)
  90. {
  91. e.printStackTrace();
  92. }
  93. }
  94.  
  95. }
Add Comment
Please, Sign In to add comment