// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DecimalFun {
    private static void test(String conurl, String un, String pw) throws SQLException {
        try(Connection con = DriverManager.getConnection(conurl, un, pw)) {
            DatabaseMetaData md = con.getMetaData();
            System.out.printf("%s %s %s %s\n", md.getDatabaseProductName(), md.getDatabaseProductVersion(), md.getDriverName(), md.getDriverVersion());
            Statement cntx = con.createStatement();
            cntx.executeUpdate("CREATE TABLE jerry (id INTEGER NOT NULL, s VARCHAR(50), m DECIMAL(20,6), PRIMARY KEY(id))");
            PreparedStatement ins = con.prepareStatement("INSERT INTO jerry VALUES (?,?,?)");
            ins.setInt(1, 123);
            ins.setString(2, "ABC");
            ins.setBigDecimal(3, new BigDecimal("1234567890.123456"));
            ins.executeUpdate();
            ins.setInt(1, 456);
            ins.setString(2, "DEF");
            ins.setBigDecimal(3, new BigDecimal("0.01"));
            ins.executeUpdate();
            ins.close();
            PreparedStatement sel = con.prepareStatement("SELECT id,s,m FROM jerry");
            ResultSet rs = sel.executeQuery();
            while(rs.next()) {
                int id = rs.getInt(1);
                String s = rs.getString(2);
                BigDecimal m = rs.getBigDecimal(3);
                System.out.printf("%d %s %22s\n", id, s, m.toPlainString());
            }
            rs.close();
            sel.close();
            cntx.executeUpdate("DROP TABLE jerry");
            cntx.close();
        }
    }
    public static void main(String[] args) throws Exception {
        test("jdbc:oracle:thin:@localhost:1521:xe", "arne", "xxxx");
        test("jdbc:db2://localhost:50000/Test", "arne", "xxxx");
        test("jdbc:sqlserver://localhost;database=Test;integratedSecurity=true;", null, null);
        test("jdbc:mysql://localhost/Test", "root", "");
        test("jdbc:derby:/work/derbytest;create=true", null, null);
        test("jdbc:h2:file:/work/h2test", "sa", "");
    }
}

/*
Output:

Oracle Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production Oracle JDBC driver 11.2.0.2.0
123 ABC      1234567890.123456
456 DEF                   0.01
DB2/NT64 SQL09074 IBM DB2 JDBC Universal Driver Architecture 3.62.56
123 ABC      1234567890.123456
456 DEF               0.010000
Microsoft SQL Server 10.50.1617 Microsoft JDBC Driver 4.0 for SQL Server 4.0.2206.100
123 ABC      1234567890.123456
456 DEF               0.010000
MySQL 5.5.12 MySQL Connector Java mysql-connector-java-5.1.36 ( Revision: 4fc1f969f740409a4e03750316df2c0e429f3dc8 )
123 ABC      1234567890.123456
456 DEF               0.010000
Apache Derby 10.11.1.2 - (1629631) Apache Derby Embedded JDBC Driver 10.11.1.2 - (1629631)
123 ABC      1234567890.123456
456 DEF               0.010000
H2 1.3.175 (2014-01-18) H2 JDBC Driver 1.3.175 (2014-01-18)
123 ABC      1234567890.123456
456 DEF               0.010000
*/
