×

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total no of tables, version, total number of views etc.

Methods

  1. getDriverName():
    In this method, used to get the name of database driver.
  2. getDriverVersion():
    In this method, used to get the version of database driver.
  3. getUserName():
    In this method, used to get the username of database.
  4. getDatabaseProductName():
    In this method, used to get the name of the product of database.
  5. getDatabaseProductVersion():
    In this method, used to get the product version of database.

Code 1:

// program for DatabaseMetaData
import java.sql.*;
class DbMetaData
{
public static void main(string args[])
{
String url =“jdbc:mysql://localhost//DB”;
Connection con = DriverManager.getConnection(url,”root”,”password”);
DatabaseMetaData DBMD = con.getMetaData();
System.out.println(“Driver Name”+DBMD.getDriverName());
System.out.println(“Driver Version”+DBMD.getDriverVersion());
System.out.println(“User Name”+DBMD.getUserName());
System.out.println(“Database Product Name”+DBMD.getDatabaseProductName());
System.out.println(“Database Product Version”+DBMD.getDatabaseProductVersion());
con.close();
}
}

By the Above program we can get the details of the Database Details like, Version , username, Product Name, Product Version.

Code 2:

import java.sql.*;
class DbMetaData2
{
public static void main(String args[])
{
Connection con = DriverManager.getConnection(url,”root”,”password”);
DatabaseMetaData DBMD = con.getMetaData();
String table[] = {“Table”};
ResultSet rs = DBMD.getTables(null,null,null,table);
while(rs.next())
{
System.out.println(rs.getString(3));
}
con.close();
}
} 

This program is used to display Number of tables present in the database.

Code 3:

import java.sql.*;
class DbMetaData2
{
public static void main(String args[])
{
Connection con = DriverManager.getConnection(url,”root”,”password”);
DatabaseMetaData DBMD = con.getMetaData();
String table[] = {“View”};
ResultSet rs = DBMD.getTables(null,null,null,table);
while(rs.next())
{
System.out.println(rs.getString(3));
}
con.close();
}
} 

This program is used to display the Views present in the database.


Related Topics

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.