1 package it.cast.jdbc;
2
3 import java.sql.Connection;
4 import java.sql.DatabaseMetaData;
5 import java.sql.Date;
6 import java.sql.ParameterMetaData;
7 import java.sql.PreparedStatement;
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10 import java.sql.Statement;
11
12 public class OtherAPI {
13
14 public static void main(String[] args) throws SQLException {
15 Object[] params = new Object[] { "zero",new Date(System.currentTimeMillis()),100f };
16 ParameterMetaTest(
17 "select * from user where name=? and birthday<? and money>? ",
18 params );
19 }
20
21 static void read() throws SQLException {
22 Connection conn = null;
23 Statement st = null;
24 ResultSet rs = null;
25
26 try {
27 conn = jdbcUtils.getConnection();
28
29 st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
30 ResultSet.CONCUR_UPDATABLE);
31
32 rs = st
33 .executeQuery("select id,name,money,birthday from user where id<5");
34
35 while (rs.next()) {
36 System.out.println(rs.getObject("id") + "/t"
37 + rs.getObject("name") + "/t"
38 + rs.getObject("birthday") + "/t");
39
40 String name = rs.getString("name");
41 if ("lisi".equals(name)) {
42 rs.updateFloat("money", 3000f);
43 rs.updateRow();
44 }
45 }
46 } finally {
47 jdbcUtils.free(rs, st, conn);
48 }
49 }
50
51 static void DBMD() throws SQLException {
52 Connection conn = jdbcUtils.getConnection();
53 DatabaseMetaData dbmd = conn.getMetaData();
54
55 System.out.println("db name:" + dbmd.getDatabaseProductName());
56 System.out.println("tx:" + dbmd.supportsTransactions());
57
58 conn.close();
59 }
60
61 static void ParameterMetaTest(String sql, Object[] params)
62 throws SQLException {
63
64 Connection conn = null;
65 PreparedStatement ps = null;
66 ResultSet rs = null;
67
68 try {
69 conn = jdbcUtils.getConnection();
70 ps = conn.prepareStatement(sql);
71
72 ParameterMetaData pmd = ps.getParameterMetaData();
73 int count = pmd.getParameterCount();
74
75 for (int i = 1; i <= count; i++) {
76 // System.out.println(pmd.getParameterClassName(i)+"\t");
77 // System.out.println(pmd.getParameterType(i)+"\t");
78 // System.out.println(pmd.getParameterTypeName(i)+"\t");
79 ps.setObject(i, params[i - 1]);
80 }
81 rs = ps.executeQuery();
82 while (rs.next()) {
83 System.out.println(rs.getObject("id") + "\t"
84 + rs.getObject("name") + "\t"
85 + rs.getObject("birthday") + "\t");
86 }
87
88 } finally {
89 jdbcUtils.free(rs, ps, conn);
90 }
91 }
92
93 }