Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class Main {
- public static void main(String[] args) throws Exception {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- PreparedStatement pstmt = null;
- try {
- // SETUP
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://...");
- stmt = conn.createStatement();
- stmt.execute("DROP TABLE IF EXISTS ttt");
- stmt.execute("CREATE TABLE ttt (value FLOAT)");
- // INPUT VALUE
- float input = 1.223473E6f;
- print(" Input", input);
- pstmt = conn.prepareStatement("INSERT INTO ttt VALUES (?)");
- pstmt.setFloat(1, input);
- pstmt.execute();
- // OUTPUT VALUES
- rs = stmt.executeQuery("SELECT value, "
- + "AVG(value) as avg, MIN(value) as min, "
- + "MAX(value) as max, value + 0 as plus, "
- + "value * 1 as mult, "
- + "cast(value as decimal) as valcast "
- + "FROM ttt;");
- rs.next();
- System.out.println();
- print("Output", rs.getFloat("value"));
- print(" AVG", rs.getFloat("avg"));
- print(" MIN", rs.getFloat("min"));
- print(" MAX", rs.getFloat("max"));
- print(" +0", rs.getFloat("plus"));
- print(" *1", rs.getFloat("mult"));
- print(" CAST", rs.getFloat("valcast"));
- } finally {
- if (stmt != null)
- stmt.close();
- if (pstmt != null)
- pstmt.close();
- if (conn != null)
- conn.close();
- }
- }
- private static void print(String name, float value) {
- System.out.println(name + ": " + value + " (0x"
- + Integer.toHexString(Float.floatToRawIntBits(value)) + ")");
- }
- Input: 1223473.0 (0x49955988)
- Output: 1223470.0 (0x49955970)
- AVG: 1223473.0 (0x49955988)
- MIN: 1223473.0 (0x49955988)
- MAX: 1223473.0 (0x49955988)
- +0: 1223473.0 (0x49955988)
- *1: 1223473.0 (0x49955988)
- CAST: 1223473.0 (0x49955988)
- System.out.println(1223473f == 1223470f); // gives false
- System.out.println(123456789f == 123456791f); // gives true (too many digits)
- mysql> create table ttt (value float);
- Query OK, 0 rows affected (0.04 sec)
- mysql> INSERT INTO ttt VALUES (1223473);
- Query OK, 1 row affected (0.00 sec)
- mysql> select * from ttt;
- +---------+
- | value |
- +---------+
- | 1223470 |
- +---------+
- 1 row in set (0.00 sec)
- mysql> SELECT * FROM ttt WHERE value = 1223470;
- Empty set (0.00 sec)
- mysql> SELECT * FROM ttt WHERE value = 1223473;
- +---------+
- | value |
- +---------+
- | 1223470 |
- +---------+
- 1 row in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement