
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java DatabaseMetaData getMaxTableNameLength() method with example.
In this article, we will learn how to use the getMaxTableNameLength() method of the DatabaseMetaData interface in Java. The getMaxTableNameLength() method is used to find the maximum number of characters allowed for a table name in the underlying database. It returns an integer value. If the result is 0, it means there is no limit or the limit is unknown.
Problem Statement
Given a database, write a Java program to find out the maximum number of characters allowed for table names in the database using the getMaxTableNameLength() method of the DatabaseMetaData interface.Input
A connection to a running database (for example, MySQL).Output
The maximum length of a table name is allowed by the underlying database (in characters).
Steps to retrieve the maximum table name length
The following are the steps to retrieve the maximum table name length
-
Make sure your database is up and running.
-
Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.
-
Get the connection object using the getConnection() method of the DriverManager class. Pass the URL of the database and, the user name, and password of a user in the database, as String variables.
-
Get the DatabaseMetaData object for the current connection using the getMetaData() method of the Connection interface.
Java Program to retrieve the maximum length of a table name:
The following JDBC example connects to the MySQL database, and retrieves and prints the maximum length of the table name, allowed by it.
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseMetadata_getMaxTableNameLength { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password"); System.out.println("Connection established......"); //Retrieving the meta data object DatabaseMetaData metaData = con.getMetaData(); //Retrieving the maximum length of the table name supported int maxLength = metaData.getMaxTableNameLength(); System.out.println("Maximum length of the table name supported: "+maxLength); } }
Output
Connection established...... Maximum length of the table name supported: 64
Code Explanation
The code starts by registering the MySQL driver using the DriverManager.registerDriver() method, The correct database driver is used. Next, a connection to the database is established using the getConnection() method, where the database URL, username, and password are provided. Once the connection is successful, the DatabaseMetaData object is obtained using the getMetaData() method. The getMaxTableNameLength() method is then called on this object to retrieve the maximum number of characters allowed for a table name. Finally, the program prints this value, which for MySQL is 64.