How to Use Methods of Column to Count Number of Columns in JDBC?
A single standalone program cannot be able to fulfill all the demands of the clients every request of the client has some different requests like the update, delete, insertJDBC is one of the famous API of the java language that is used to connect relational databases working on the Structured Query Language with the java programs.JDBC along with the database driver is capable of accessing databases and spreadsheets. The enterprise data stored in a relational database(RDB) can be accessed with the help of JDBC APIs.
The main tasks of the JDBC API
- Establish a connection with a database
- Send SQL statements to the database server.
- Process the result obtained.
Illustration: Suppose MySQL Client
- Database : GFG
- Table : Aayush
Approach: Steps for connectivity between Java program and database
- Importing the database files.
- Load and register drivers to java program.
- Establish the connections.
- Create a statement.
- Execute the query.
- Process the results.
- Close the connections.
Now, discussing the above steps in order figuring out how to use methods of the column to count no of columns in JDBC
1. Loading and registering drivers: To begin with, you first need to load the driver or register it before using it in the program. Registration is to be done once in your program. You can register a driver in one of the two ways is by using Class.forName() method. It is used to load the driver’s class file into memory at the runtime and there is no need of using new or creation of an object. In order to illustrate for Oracle driver it is as follows:
2. Establish the connections: After loading the driver, establish connections by creating an object of Connection class of main class as depicted:
Connection con = DriverManager.getConnection(url,user,password)
- user: username from which your SQL command prompt can be accessed.
- password: password from which your SQL command prompt can be accessed.
- con: Connection class object is a reference to the Connection interface.
- URL: Uniform Resource Locator.
If Oracle is used as a database then it is treated in a similar manner but with a little gimmick to it as follows.
- The driver used @localhost is the IP address where the database is stored.
- 1521 is the port number.
- The service provider is ‘xe’ here.
All 3 parameters here are of String type and are to be declared by the programmer before calling the function and using this can be referred from the final code. It can be created as follows:
String url = “ jdbc:oracle:thin:@localhost:1521:xe” OR String mysqlUrl = "jdbc:mysql://localhost/Databasename";
3. Create a statement: Once a connection is established you can interact with the database. The JDBCStatement, Callable Statement and PreparedStatement interfaces define the methods that enable you to send SQL commands and receive data from your database. Use of JDBC Statement is as follows. Here, the con is a reference to the Connection interface used in the previous step.
Statement st = con.createStatement();
4. Execute the query: Now comes the most important part i.e executing the query. The query here is an SQL Query. Now we know we can have multiple types of queries. Some of them are as follows:
- The query for updating/inserting table in a database.
- The query for retrieving data.
The executeQuery() method of Statement interface is used to execute queries of retrieving values from the database. This method returns the object of ResultSet that can be used to get all the records of a table. The executeUpdate(sql query) method of the Statement interface is used to execute queries of updating/inserting.
Same table is used in the implementation part. So, the above output is of corresponding table generated below on which count is called.
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.