参数的元数据信息&数据库的元数据信息

参数的元数据信息&数据库的元数据信息
 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 }
OtherAPI

 

参数的元数据信息&数据库的元数据信息
  1 package it.cast.jdbc;
  2 
  3 import java.sql.CallableStatement;
  4 import java.sql.Connection;
  5 import java.sql.DriverManager;
  6 import java.sql.PreparedStatement;
  7 import java.sql.ResultSet;
  8 import java.sql.SQLException;
  9 import java.sql.Statement;
 10 
 11 public class jdbcUtils {
 12 
 13     private static String url = "jdbc:mysql://localhost:3306/jdbc?generateSimpleParameterMetadata=true";
 14     private static String user = "root";
 15     private static String password = "123";
 16 
 17     private jdbcUtils() {
 18 
 19     }
 20 
 21     static {
 22         try {
 23             Class.forName("com.mysql.jdbc.Driver");
 24         } catch (ClassNotFoundException e) {
 25             e.printStackTrace();
 26         }
 27     }
 28 
 29     public static Connection getConnection() throws SQLException {
 30         return DriverManager.getConnection(url, user, password);
 31     }
 32 
 33     public static void free(ResultSet rs, Statement st, Connection conn) {
 34 
 35         try {
 36             if (rs != null)
 37                 rs.close();
 38         } catch (SQLException e) {
 39             e.printStackTrace();
 40         } finally {
 41 
 42             try {
 43                 if (st != null)
 44                     st.close();
 45             } catch (SQLException e) {
 46                 e.printStackTrace();
 47             } finally {
 48 
 49                 try {
 50                     if (conn != null)
 51                         conn.close();
 52                 } catch (SQLException e) {
 53                     e.printStackTrace();
 54                 }
 55             }
 56 
 57         }
 58 
 59     }
 60     
 61     public static void free(ResultSet rs, PreparedStatement ps, Connection conn) {
 62 
 63         try {
 64             if (rs != null)
 65                 rs.close();
 66         } catch (SQLException e) {
 67             e.printStackTrace();
 68         } finally {
 69 
 70             try {
 71                 if (ps != null)
 72                     ps.close();
 73             } catch (SQLException e) {
 74                 e.printStackTrace();
 75             } finally {
 76 
 77                 try {
 78                     if (conn != null)
 79                         conn.close();
 80                 } catch (SQLException e) {
 81                     e.printStackTrace();
 82                 }
 83             }
 84 
 85         }
 86 
 87     }
 88     public static void free(ResultSet rs, CallableStatement cs, Connection conn) {
 89 
 90         try {
 91             if (rs != null)
 92                 rs.close();
 93         } catch (SQLException e) {
 94             e.printStackTrace();
 95         } finally {
 96 
 97             try {
 98                 if (cs != null)
 99                     cs.close();
100             } catch (SQLException e) {
101                 e.printStackTrace();
102             } finally {
103 
104                 try {
105                     if (conn != null)
106                         conn.close();
107                 } catch (SQLException e) {
108                     e.printStackTrace();
109                 }
110             }
111 
112         }
113 
114     }
115 }
jdbcUtils

 

参数的元数据信息&数据库的元数据信息

上一篇:rhel5.8安装oracle 10g ASM


下一篇:数据库语句学习(union语句)