JDBC常用API的代码示例.docx

上传人:b****4 文档编号:12102550 上传时间:2023-04-17 格式:DOCX 页数:18 大小:20.81KB
下载 相关 举报
JDBC常用API的代码示例.docx_第1页
第1页 / 共18页
JDBC常用API的代码示例.docx_第2页
第2页 / 共18页
JDBC常用API的代码示例.docx_第3页
第3页 / 共18页
JDBC常用API的代码示例.docx_第4页
第4页 / 共18页
JDBC常用API的代码示例.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

JDBC常用API的代码示例.docx

《JDBC常用API的代码示例.docx》由会员分享,可在线阅读,更多相关《JDBC常用API的代码示例.docx(18页珍藏版)》请在冰豆网上搜索。

JDBC常用API的代码示例.docx

JDBC常用API的代码示例

Java代码

1.package com.database.manager;  

2.  

3.import java.sql.Connection;  

4.import java.sql.DatabaseMetaData;  

5.import java.sql.DriverManager;  

6.import java.sql.ResultSet;  

7.import java.sql.SQLException;  

8.  

9./** 

10. * @author daoger 

11. *  

12. * 2009-9-24 

13. */  

14.public class DatabaseMetaDateApplication  

15.{  

16.    private DatabaseMetaData dbMetaData = null;  

17.  

18.    private Connection con = null;  

19.  

20.    private void getDatabaseMetaData()  

21.    {  

22.        try  

23.        {  

24.            if (dbMetaData == null)  

25.            {  

26.                Class.forName("oracle.jdbc.driver.OracleDriver");  

27.                String url = "jdbc:

oracle:

thin:

@192.168.0.2:

1521:

×××";  

28.                String user = "×××";  

29.                String password = "×××";  

30.                con = DriverManager.getConnection(url, user, password);  

31.                dbMetaData = con.getMetaData();  

32.            }  

33.        } catch (ClassNotFoundException e)  

34.        {  

35.            // TODO:

 handle ClassNotFoundException  

36.            e.printStackTrace();  

37.        } catch (SQLException e)  

38.        {  

39.            // TODO:

 handle SQLException  

40.            e.printStackTrace();  

41.        }  

42.    }  

43.  

44.    public void colseCon()  

45.    {  

46.        try  

47.        {  

48.            if (con !

= null)  

49.            {  

50.                con.close();  

51.            }  

52.        } catch (SQLException e)  

53.        {  

54.            // TODO:

 handle SQLException  

55.            e.printStackTrace();  

56.        }  

57.    }  

58.  

59.    /** 

60.     * 获得数据库的一些相关信息 

61.     */  

62.    public void getDataBaseInformations()  

63.    {  

64.        try  

65.        {  

66.            System.out.println("URL:

" + dbMetaData.getURL() + ";");  

67.            System.out.println("UserName:

" + dbMetaData.getUserName() + ";");  

68.            System.out.println("isReadOnly:

" + dbMetaData.isReadOnly() + ";");  

69.            System.out.println("DatabaseProductName:

" + dbMetaData.getDatabaseProductName() + ";");  

70.            System.out.println("DatabaseProductVersion:

" + dbMetaData.getDatabaseProductVersion() + ";");  

71.            System.out.println("DriverName:

" + dbMetaData.getDriverName() + ";");  

72.            System.out.println("DriverVersion:

" + dbMetaData.getDriverVersion());  

73.        } catch (SQLException e)  

74.        {  

75.            // TODO:

 handle SQLException  

76.            e.printStackTrace();  

77.        }  

78.    }  

79.  

80.    /** 

81.     * 获得该用户下面的所有表 

82.     */  

83.    public void getAllTableList(String schemaName)  

84.    {  

85.        try  

86.        {  

87.            // table type. Typical types are "TABLE", "VIEW", "SYSTEM  

88.            // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",  

89.            // "SYNONYM".  

90.            String[] types =  

91.            { "TABLE" };  

92.            ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);  

93.            while (rs.next())  

94.            {  

95.                String tableName = rs.getString("TABLE_NAME");  

96.                // table type. Typical types are "TABLE", "VIEW", "SYSTEM  

97.                // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",  

98.                // "SYNONYM".  

99.                String tableType = rs.getString("TABLE_TYPE");  

100.                // explanatory comment on the table  

101.                String remarks = rs.getString("REMARKS");  

102.                System.out.println(tableName + "-" + tableType + "-" + remarks);  

103.            }  

104.        } catch (SQLException e)  

105.        {  

106.            // TODO:

 handle SQLException  

107.            e.printStackTrace();  

108.        }  

109.    }  

110.  

111.    /** 

112.     * 获得该用户下面的所有视图 

113.     */  

114.    public void getAllViewList(String schemaName)  

115.    {  

116.        try  

117.        {  

118.            String[] types =  

119.            { "VIEW" };  

120.            ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);  

121.            while (rs.next())  

122.            {  

123.                String viewName = rs.getString("TABLE_NAME");  

124.                // table type. Typical types are "TABLE", "VIEW", "SYSTEM  

125.                // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",  

126.                // "SYNONYM".  

127.                String viewType = rs.getString("TABLE_TYPE");  

128.                // explanatory comment on the table  

129.                String remarks = rs.getString("REMARKS");  

130.                System.out.println(viewName + "-" + viewType + "-" + remarks);  

131.            }  

132.        } catch (SQLException e)  

133.        {  

134.            // TODO:

 handle SQLException  

135.            e.printStackTrace();  

136.        }  

137.    }  

138.  

139.    /** 

140.     * 获得数据库中所有方案名称 

141.     */  

142.    public void getAllSchemas()  

143.    {  

144.        try  

145.        {  

146.            ResultSet rs = dbMetaData.getSchemas();  

147.            while (rs.next())  

148.            {  

149.                String tableSchem = rs.getString("TABLE_SCHEM");  

150.                System.out.println(tableSchem);  

151.            }  

152.        } catch (SQLException e)  

153.        {  

154.            // TODO:

 handle SQLException  

155.            e.printStackTrace();  

156.        }  

157.    }  

158.  

159.    /** 

160.     * 获得表或视图中的所有列信息 

161.     */  

162.    public void getTableColumns(String schemaName, String tableName)  

163.    {  

164.        try  

165.        {  

166.            ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");  

167.            while (rs.next())  

168.            {  

169.                // table catalog (may be null)  

170.                String tableCat = rs.getString("TABLE_CAT");  

171.                // table schema (may be null)  

172.                String tableSchemaName = rs.getString("TABLE_SCHEM");  

173.                // table name  

174.                String tableName_ = rs.getString("TABLE_NAME");  

175.                // column name  

176.                String columnName = rs.getString("COLUMN_NAME");  

177.                // SQL type from java.sql.Types  

178.                int dataType = rs.getInt("DATA_TYPE");  

179.                // Data source dependent type name, for a UDT the type name is  

180.                // fully qualified  

181.                String dataTypeName = rs.getString("TYPE_NAME");  

182.                // table schema (may be null)  

183.                int columnSize = rs.getInt("COLUMN_SIZE");  

184.                // the number of fractional digits. Null is returned for data  

185.                // types where DECIMAL_DIGITS is not applicable.  

186.                int decimalDigits = rs.getInt("DECIMAL_DIGITS");  

187.                // Radix (typically either 10 or 2)  

188.                int numPrecRadix = rs.getInt("NUM_PREC_RADIX");  

189.                // is NULL allowed.  

190.                int nullAble = rs.getInt("NULLABLE");  

191.                // comment describing column (may be null)  

192.                String remarks = rs.getString("REMARKS");  

193.                // default value for the column, which should be interpreted as  

194.                // a string when the value is enclosed in single quotes (may be  

195.                // null)  

196.                String columnDef = rs.getString("COLUMN_DEF");  

197.                //                

198.                int sqlDataType = rs.getInt("SQL_DATA_TYPE");  

199.                //                

200.                int sqlDatetimeSub = rs.getInt("SQL_DATETIME_SUB");  

201.                // for char types the maximum number of bytes in the column  

202.                int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH");  

203.                // index of column in table (starting at 1)  

204.                int ordinalPosition = rs.getInt("ORDINAL_POSITION");  

205.                // ISO rules are used to determine the nullability for a column.  

206.                // YES --- if the parameter can include NULLs;  

207.                // NO --- if the parameter cannot include NULLs  

208.                // empty string --- if the nullability for the parameter is  

209.                // unknown  

210.                String isNullAble = rs.getString("IS_NULLABLE");  

211.                // Indicates whether this column is auto incremented  

212.                // YES --- if the column is auto incremented  

213.                // NO --- if the column is not auto incremented  

214.                // empty string --- if it cannot be determined whether the  

215.                // column is auto incremented parameter is unknown  

216.                String isAutoincrement = rs.getString("IS_AUTOINCREMENT");  

217.                System.out.println(tableCat + "-" + tableSchemaName + "-" + tableName_ + "-" + columnName + "-"  

218.                        + dataType + "-" + dataTypeName + "-" + columnSize + "-" + decimalDigits + "-" + numPrecRadix  

219.                        + "-" + nullAble + "-" + remarks + "-" + columnDef + "-" + sqlDataType + "-" + sqlDatetimeSub  

220.             

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1