1 5 package org.h2.test.jdbc; 6 7 import java.math.BigDecimal ; 8 import java.sql.Connection ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 13 import org.h2.test.TestBase; 14 15 public class TestZloty extends TestBase { 16 17 public void test() throws Exception { 18 testZloty(); 19 testModifyBytes(); 20 } 21 22 private static class ZlotyBigDecimal extends BigDecimal { 23 24 public ZlotyBigDecimal(String s) { 25 super(s); 26 } 27 28 private static final long serialVersionUID = -8004563653683501484L; 29 30 public int compareTo(BigDecimal bd) { 31 return -super.compareTo(bd); 32 } 33 34 } 35 36 private void testModifyBytes() throws Exception { 37 deleteDb("zloty"); 38 Connection conn = getConnection("zloty"); 39 conn.createStatement().execute("CREATE TABLE TEST(ID INT, DATA BINARY)"); 40 PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)"); 41 byte[] shared = new byte[1]; 42 prep.setInt(1, 0); 43 prep.setBytes(2, shared); 44 prep.execute(); 45 shared[0] = 1; 46 prep.setInt(1, 1); 47 prep.setBytes(2, shared); 48 prep.execute(); 49 ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST ORDER BY ID"); 50 rs.next(); 51 check(rs.getInt(1), 0); 52 check(rs.getBytes(2)[0], 0); 53 rs.next(); 54 check(rs.getInt(1), 1); 55 check(rs.getBytes(2)[0], 1); 56 rs.getBytes(2)[0] = 2; 57 check(rs.getBytes(2)[0], 1); 58 checkFalse(rs.next()); 59 conn.close(); 60 } 61 62 66 private void testZloty() throws Exception { 67 deleteDb("zloty"); 68 Connection conn = getConnection("zloty"); 69 conn.createStatement().execute("CREATE TABLE TEST(ID INT, AMOUNT DECIMAL)"); 70 PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)"); 71 prep.setInt(1, 1); 72 prep.setBigDecimal(2, new BigDecimal ("10.0")); 73 prep.execute(); 74 prep.setInt(1, 2); 75 try { 76 prep.setBigDecimal(2, new ZlotyBigDecimal("11.0")); 77 prep.execute(); 78 error("unexpected success"); 79 } catch(SQLException e) { 80 checkNotGeneralException(e); 81 } 82 83 prep.setInt(1, 3); 84 try { 85 BigDecimal crappyVal=new BigDecimal ("12.100000") { 86 private static final long serialVersionUID = -7909023971521750844L; 87 public String toString() { return "12,100000 EURO"; } 88 }; 89 prep.setBigDecimal(2, crappyVal); 90 prep.execute(); 91 error("unexpected success"); 92 } catch(SQLException e) { 93 checkNotGeneralException(e); 94 } 95 96 conn.close(); 97 } 98 99 } 100 | Popular Tags |