In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. JDBC metadata
Can be used in jdbc: database metadata, parameter metadata, result set metadata
(1) Database metadata
You can get the DatabaseMetaData object through the getMetaData () method of the Connection object. The DatabaseMetaData object contains the meta-information of the database.
DatabaseMetaData java.sql.Connection.getMetaData ()
Retrieves a DatabaseMetaData object that contains metadata about the database. The metadata includes information about the database's tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on.
Java.sql.DatabaseMetaData
(1) this interface is implemented by the driver manufacturer, and the capabilities of the database can be obtained through this interface.
This interface is implemented by driver vendors to let users know the capabilities of a Database
(2) different DBMS provides different features, different ways to implement them, and different data types.
Different relational DBMSs often support different features, implement features in different ways, and use different data types.
GetURL (): returns a String class object that represents the URL of the database.
GetUserName (): returns the user name that connects to the current database management system.
GetDatabaseProductName (): returns the product name of the database.
GetDatabaseProductVersion (): returns the version number of the database.
GetDriverName (): returns the name of the driver.
GetDriverVersion (): returns the version number of the driver.
IsReadOnly (): returns a Boolean value indicating whether the database only allows read operations.
(2) Parameter metadata
Java.sql.ParameterMetaData
An object that can be used to get information about the types and properties for each parameter marker in a PreparedStatement object.
The number of parameters in the ParameterMetaData object can be obtained through the getParameterCount () method of the PreparedStatement object.
Int java.sql.ParameterMetaData.getParameterCount ()
Retrieves the number of parameters in the PreparedStatement object.
(3) result set metadata
The ResultSetMetaData object is obtained through the getMetaData () method of the ResultSet object.
ResultSetMetaData java.sql.ResultSet.getMetaData ()
Retrieves the number, types and properties of this ResultSet object's columns.
Java.sql.ResultSetMetaData
An object that can be used to get information about the types and properties of the columns in a ResultSet object.
1.1.Database metadata package com.rk.metadata;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.SQLException;import com.rk.utils.JDBCUtil;//1. Database metadata public class Demo01 {public static void main (String [] args) throws SQLException {/ / get connection Connection conn = JDBCUtil.getConnection (); / / get database metadata DatabaseMetaData metaData = conn.getMetaData () / / alt + shift + L quickly gets the return value System.out.println ("DatabaseProductName:" + metaData.getDatabaseProductName ()) of the method; / / returns the product name of the database. System.out.println ("DatabaseProductVersion:" + metaData.getDatabaseProductVersion ()); / / returns the version number of the database System.out.println ("DriverName:" + metaData.getDriverName ()); / / returns the driver name System.out.println ("getDriverVersion:" + metaData.getDriverVersion ()); / / returns the driver version number System.out.println ("URL:" + metaData.getURL ()). / / returns a String class object that represents the URL System.out.println of the database ("UserName:" + metaData.getUserName ()); / / returns the user name System.out.println ("ReadOnly:" + metaData.isReadOnly ()) that connects to the current database management system. / / returns a Boolean value indicating whether the database only allows read operations JDBCUtil.closeQuietly (conn);}}
1.2.Parametric metadata package com.rk.metadata;import java.sql.Connection;import java.sql.ParameterMetaData;import java.sql.PreparedStatement;import java.sql.SQLException;import com.rk.utils.JDBCUtil;//2. Parameter metadata public class Demo02 {public static void main (String [] args) throws SQLException {/ / get connection Connection conn = JDBCUtil.getConnection (); / / SQL String sql = "SELECT * FROM T_Dogs WHERE Name=?" And Age >? "; PreparedStatement pstmt = conn.prepareStatement (sql); / / Parameter metadata ParameterMetaData parameterMetaData = pstmt.getParameterMetaData (); / / get the number of parameters int count = parameterMetaData.getParameterCount (); System.out.println (" count: "+ count) JDBCUtil.closeQuietly (pstmt); JDBCUtil.closeQuietly (conn);}}
1.3.Result set metadata package com.rk.metadata;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import com.rk.utils.JDBCUtil;//3. Result set metadata public class Demo03 {public static void main (String [] args) throws SQLException {/ / get connection Connection conn = JDBCUtil.getConnection (); String sql = "SELECT * FROM T_Dogs"; PreparedStatement pstmt = conn.prepareStatement (sql); ResultSet rs = pstmt.executeQuery () / / get the result set metadata (goal: get the column name from the result set metadata) ResultSetMetaData rs_metaData = rs.getMetaData () / / iterate over the result of each line while (rs.next ()) {/ / 1. Get the number of columns int count = rs_metaData.getColumnCount (); / / 2. Traverses to get the name of the column of each column, for (int icolumns 0 / 0) {/ / loop to assign the parameter value for (int icolumns 0 / i0) {for (int icolumns 0) I get the number of columns int columnCount = rsmd.getColumnCount (); / / 6. Traversing rs while (rs.next ()) {/ / the object to be encapsulated T t = clazz.newInstance (); / / 7. Iterate through each column of each row, encapsulating the data for (int iaddress 0 / i0)? list.get (0): null;} public void update (DogInfo dog) {String sql = "UPDATE T_Dogs SET Name=?,Age=?,BirthDay=?" WHERE Id=? "; super.update (sql, dog.getName (), dog.getAge (), dog.getBirthDay (), dog.getId ());} public void delete (int id) {String sql =" DELETE FROM T_Dogs WHERE Id=? "; super.update (sql, id);}}
Demo04.java
Package com.rk.metadata;import java.util.Date;import java.util.List;import com.rk.dao.DogDAO;import com.rk.entity.DogInfo;public class Demo04 {public static void main (String [] args) {/ / Save / / DogInfo d = new DogInfo (); / / d.setName ("woof"); / / d.setAge (2) / / d.setBirthDay (new Date ()); / DogDAO dao = new DogDAO (); / / dao.save (d); / / find all / / DogDAO dao = new DogDAO (); / / List list = dao.findAll () / / for (DogInfo dog: list) / / {/ / System.out.println (dog); / /} / / find specific id records / / DogDAO dao = new DogDAO (); / / DogInfo d = dao.findById (1) / / System.out.println (d); / / Update / / DogInfo d = new DogInfo (); / / d.setId (1); / / d.setName ("prosperous wealth"); / / d.setAge (3); / / d.setBirthDay (new Date ()) / / DogDAO dao = new DogDAO (); / / dao.update (d); / / delete DogDAO dao = new DogDAO (); dao.delete (1);}}
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.