- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program to get the list of all the supported datatypes in JDBC?
The class named Types of the java.sql package contains the constants that represents the SQL datatypes. All these datatypes are represented by unique integer values.
Retrieving the integer values from the Types class
To print all the class names and values of the constants in java.sql.Types class −
- Retrieve all the fields in Types class − The getFields() method of the class Class returns an array which holds all the fileds (public) of the class/interface represented by the current Class object.Using this method retrieve the array of fileds of the Types class as shown below −
Field[] fields = java.sql.Types.class.getFields();
- Retrieve the name and value of each field − The getName() of the Field class returns the name of the filed represented by the current field object.
- Similarly, the get() method of the Field class returns the value of the filed represented by the current filed object.
- Using these two methods print the name and value of each field of the Types class as shown below −
for(int i = 0; i<fields.length; i++) { //Retrieving the name of the field String name = fields[i].getName(); //Retrieving the value of the field int value = (int) fields[i].get(null); System.out.println(name+" : "+value); }
Example
Following Java program retrieves and prints all the datatypes and their values represented by the java.sql.Types class.
import java.sql.Connection; import java.sql.DriverManager; import java.lang.reflect.Field; public class ListOfDatatypes { public static void main(String args[])throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampledatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established: "+con); //Retrieving all the fields Field[] fields = java.sql.Types.class.getFields(); for(int i = 0; i<fields.length; i++) { //Retrieving the name of the field String name = fields[i].getName(); //Retrieving the value of the field int value = (int) fields[i].get(null); System.out.println(name+" : "+value); } } }
Output
Connection established: com.mysql.jdbc.JDBC4Connection@4fccd51b BIT : -7 TINYINT : -6 SMALLINT : 5 INTEGER : 4 BIGINT : -5 FLOAT : 6 REAL : 7 DOUBLE : 8 NUMERIC : 2 DECIMAL : 3 CHAR : 1 VARCHAR : 12 LONGVARCHAR : -1 DATE : 91 TIME : 92 TIMESTAMP : 93 BINARY : -2 VARBINARY : -3 LONGVARBINARY : -4 NULL : 0 OTHER : 1111 JAVA_OBJECT : 2000 DISTINCT : 2001 STRUCT : 2002 ARRAY : 2003 BLOB : 2004 CLOB : 2005 REF : 2006 DATALINK : 70 BOOLEAN : 16 ROWID : -8 NCHAR : -15 NVARCHAR : -9 LONGNVARCHAR : -16 NCLOB : 2011 SQLXML : 2009 REF_CURSOR : 2012 TIME_WITH_TIMEZONE : 2013 TIMESTAMP_WITH_TIMEZONE : 2014
Advertisements