Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) throws Exception {
  10.  
  11. Connection conn = null;
  12. Statement stmt = null;
  13. ResultSet rs = null;
  14. PreparedStatement pstmt = null;
  15.  
  16. try {
  17. // SETUP
  18. Class.forName("com.mysql.jdbc.Driver");
  19. conn = DriverManager.getConnection("jdbc:mysql://...");
  20.  
  21. stmt = conn.createStatement();
  22. stmt.execute("DROP TABLE IF EXISTS ttt");
  23.  
  24. stmt.execute("CREATE TABLE ttt (value FLOAT)");
  25.  
  26. // INPUT VALUE
  27. float input = 1.223473E6f;
  28. print(" Input", input);
  29. pstmt = conn.prepareStatement("INSERT INTO ttt VALUES (?)");
  30. pstmt.setFloat(1, input);
  31. pstmt.execute();
  32.  
  33. // OUTPUT VALUES
  34. rs = stmt.executeQuery("SELECT value, "
  35. + "AVG(value) as avg, MIN(value) as min, "
  36. + "MAX(value) as max, value + 0 as plus, "
  37. + "value * 1 as mult, "
  38. + "cast(value as decimal) as valcast "
  39. + "FROM ttt;");
  40. rs.next();
  41. System.out.println();
  42. print("Output", rs.getFloat("value"));
  43. print(" AVG", rs.getFloat("avg"));
  44. print(" MIN", rs.getFloat("min"));
  45. print(" MAX", rs.getFloat("max"));
  46. print(" +0", rs.getFloat("plus"));
  47. print(" *1", rs.getFloat("mult"));
  48. print(" CAST", rs.getFloat("valcast"));
  49.  
  50. } finally {
  51. if (stmt != null)
  52. stmt.close();
  53. if (pstmt != null)
  54. pstmt.close();
  55. if (conn != null)
  56. conn.close();
  57. }
  58.  
  59. }
  60.  
  61. private static void print(String name, float value) {
  62. System.out.println(name + ": " + value + " (0x"
  63. + Integer.toHexString(Float.floatToRawIntBits(value)) + ")");
  64. }
  65.  
  66. Input: 1223473.0 (0x49955988)
  67.  
  68. Output: 1223470.0 (0x49955970)
  69. AVG: 1223473.0 (0x49955988)
  70. MIN: 1223473.0 (0x49955988)
  71. MAX: 1223473.0 (0x49955988)
  72. +0: 1223473.0 (0x49955988)
  73. *1: 1223473.0 (0x49955988)
  74. CAST: 1223473.0 (0x49955988)
  75.  
  76. System.out.println(1223473f == 1223470f); // gives false
  77. System.out.println(123456789f == 123456791f); // gives true (too many digits)
  78.  
  79. mysql> create table ttt (value float);
  80. Query OK, 0 rows affected (0.04 sec)
  81.  
  82. mysql> INSERT INTO ttt VALUES (1223473);
  83. Query OK, 1 row affected (0.00 sec)
  84.  
  85. mysql> select * from ttt;
  86. +---------+
  87. | value |
  88. +---------+
  89. | 1223470 |
  90. +---------+
  91. 1 row in set (0.00 sec)
  92.  
  93. mysql> SELECT * FROM ttt WHERE value = 1223470;
  94. Empty set (0.00 sec)
  95.  
  96. mysql> SELECT * FROM ttt WHERE value = 1223473;
  97. +---------+
  98. | value |
  99. +---------+
  100. | 1223470 |
  101. +---------+
  102. 1 row in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement