Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. public class Demo1 {
  2. private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  3. private static final String DB_CONNECTION ="jdbc:mysql://localhost:3306/db?autoReconnect=true";
  4. private static final String DB_USER = "root";
  5. private static final String DB_PASSWORD = "root";
  6.  
  7.  
  8. private static void insertRecord() throws SQLException {
  9. try{
  10.  
  11. Connection dbConnection = null;
  12. PreparedStatement preparedStatement = null;
  13. PreparedStatement ps = dbConnection.prepareStatement("SELECT * FROM CMD2");
  14.  
  15. //String device_id;
  16. // TODO Auto-generated method stub
  17. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  18. Date date = new Date();
  19. // List<CMD1> arrObj = liveInfo.getTodayFrames(device_id, df.format(date));
  20. df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  21.  
  22. double S ;
  23. double SH ;
  24.  
  25. Date previousDate = null,
  26. currentTime = null;
  27.  
  28. if (previousDate != null) {
  29.  
  30. float timeDiff = 2;
  31. double S1;
  32. S1 =(Float.parseFloat(cmd.getA())*(cmd.getB()))/1000;
  33. S = S + S1;
  34.  
  35. double SH1;
  36. S1 =(Float.parseFloat(cmd.getA())* timeDiff )/60;
  37. SH = SH + SH1;
  38.  
  39. previousDate = df.parse(cmd.getDate() + " " + cmd.getTime());
  40.  
  41. } else {
  42.  
  43. previousDate = df.parse(cmd.getDate() + " " + cmd.getTime());
  44. S =0 ;
  45. SH = 0;
  46.  
  47. }
  48.  
  49.  
  50.  
  51. System.out.println(S+" "+SH+" ");
  52.  
  53. // PreparedStatement ps1 = dbConnection.prepareStatement("INSERT INTO cmd2( date, time) VALUES(?, ?) FROM CMD");
  54.  
  55. ps.setString(1, S);
  56. ps.setString(3, SH);
  57.  
  58. ps.execute();
  59. System.out.println("Record is inserted into table!");
  60.  
  61. } catch (SQLException e) {
  62. System.out.println(e.getMessage());
  63.  
  64. } finally{
  65. }
  66.  
  67. }
  68.  
  69. private static Connection getDBConnection() {
  70.  
  71. Connection dbConnection = null;
  72.  
  73. try {
  74.  
  75. Class.forName(DB_DRIVER);
  76.  
  77. } catch (ClassNotFoundException e) {
  78.  
  79. System.out.println(e.getMessage());
  80.  
  81. }
  82.  
  83. try {
  84.  
  85. dbConnection = DriverManager.getConnection(
  86. DB_CONNECTION, DB_USER,DB_PASSWORD);
  87. return dbConnection;
  88.  
  89. }
  90. catch (SQLException e) {
  91. System.out.println(e.getMessage());
  92.  
  93. }
  94. return dbConnection;
  95.  
  96. }
  97.  
  98. public static void main(String[] argv) {
  99.  
  100. try {
  101. insertRecord();
  102. }
  103. catch (SQLException e) {
  104. System.out.println(e.getMessage());
  105.  
  106. }
  107.  
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement