Java Program to Use Methods of Column to Get Column Name in JDBC
Java Database Connectivity or JDBC is a Java API (application programming interface) used to perform database operations through Java application. It allows the execution of SQL commands for the creation of tables and data manipulation through Java application. Java supports java.sql package which contains inbuilt methods for accessing and processing data stored in a database. The methods in java.sql throw SQL exceptions hence the code block must be either placed within a try block and let the catch block handle exceptions if any else add throws clause to the main method.
- The forName() is a static method of class which is used to load the driver.
- Next the connection to the database is established using the getConnection() method of DriverManager class. The getConnection() accepts the URLs, username and password to establish a connection. The methods defined in the Statement interface is used to interact with the database.
- First, the table is created and then records are inserted into the table.
- The sql commands are batched together using addBatch() and executed all at once using executeBatch() method.
- The records in the table are fetched into the ResultSet.
- The getMetaData() method of ResultSet is used to get the metadata of the records fetched into the ResultSet.
- The getColumnCount() and getColumnName() methods of ResultSetMetaData interface is used to get the number of columns and name of each column in the table.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.