Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using metadata to analyze databases in Java Foundation Series 15:JDBC

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

(1)DatabaseMetaData:

package javase.jdbc;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ResultSet;import java.sql.SQLException;public class DatabaseMetaDataDemo { public static void main(String[] args) { Connection connection = JDBCConnection.getConnection(); try { DatabaseMetaData dMetaData = connection.getMetaData(); //instantiate metadata System.out.println("database name: " + dMetaData.getDatabaseProductName()); System.out.println("Database version: " + dMetaData.getDatabaseProductVersion()); ResultSet resultSet = dMetaData.getPrimaryKeys(null, null, "users"); while (resultSet.next()) { System.out.println("Table Category: " + resultSet.getString(1)); System.out.println("table schema: " + resultSet.getString(2)); System.out.println("table name: " + resultSet.getString(3)); System.out.println("Column Name: " + resultSet.getString(4)); System.out.println("PK: " + resultSet.getString(5)); System.out.println("Primary key name: " + resultSet.getString(6)); } } catch (SQLException e) { e.printStackTrace(); } }}

Output:

Database Name: MySQL Database Version: 5.5.19 Table Category: jdbcdemo Table Mode: null Table Name: users Column Name: id Primary Key: 1 Primary Key Name: PRIMARY

(2)ResultSetMetaData:

package javase.jdbc;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSetMetaData;import java.sql.SQLException;public class ResultSetMetaDataDemo { public static void main(String[] args) { String sql = "SELECT id,username,password,classId FROM users"; Connection connection = JDBCConnection.getConnection(); try { PreparedStatement pStatement = connection.prepareStatement(sql); ResultSetMetaData rSetMetaData = pStatement.getMetaData(); System.out.println("Return data has" + rSetMetaData.getColumnCount() + "fields"); if (rSetMetaData.isAutoIncrement(1)) System.out.println(rSetMetaData.getColumnName(1) + "field is self-increasing"); } catch (SQLException e) { e.printStackTrace(); } }}

Output:

The returned data has 4 fields. The id field is self-increasing.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report