A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/jdbc_column_methods.htm below:

How to use different methods of column to Count no of columns, get column name, get column type in Java

How to use different methods of column to Count no of columns, get column name, get column type in Java Problem Description

How to use different methods of column to Count no of columns, get column name, get column type etc?

Solution

Following example uses getColumnCount, getColumnName, getColumnTypeName, getColumnDisplaySize methods to get the no of columns, name of the column or type of the column in the table.

import java.sql.*;

public class jdbcConn {
   public static void main(String[] args) throws Exception {
      Class.forName("org.apache.derby.jdbc.ClientDriver");
      Connection con = DriverManager.getConnection(
         "jdbc:derby://localhost:1527/testDb","name","pass");
      
      Statement stmt = con.createStatement();
      String query = "select * from emp order by name";
      
      ResultSet rs = stmt.executeQuery(query);
      ResultSetMetaData rsmd = rs.getMetaData();
      
      System.out.println("no of columns in the table = "+ rsmd.getColumnCount());
      System.out.println("Name of the first column "+ rsmd.getColumnName(1));
      System.out.println("Type of the second column "+ rsmd.getColumnTypeName(2));
      System.out.println("No of characters in 3rd column "+ rsmd.getColumnDisplaySize(2));
   }
}
Result

The above code sample will produce the following result. The result may vary.

no of columns in the table = 3
Name of the first columnID
Type of the second columnVARCHAR
No of characters in 3rd column20

java_jdbc.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4