If you want to get the desc (description) of a table using Java JDBC, you can do that by running a Statment query,
Query:desc students;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class JDBCTableDescExample {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url ="jdbc:mysql://localhost:3306/my_uat";
String userName="root";
String password ="root123";
String insertQuery ="desc students";
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
ResultSet rs = preparedStatement.executeQuery();
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int colCount = resultSetMetaData.getColumnCount();
for(int i=1;i<=colCount;i++) {
System.out.print(resultSetMetaData.getColumnName(i)+"\t |");
}
System.out.println("");
while(rs.next()) {
for(int i=1;i<=colCount;i++) {
System.out.print(rs.getString(i)+"\t |");
}
System.out.println("");
}
}
}
Output:
Field |Type |Null |Key |Default |Extra |
student_id |int |NO |PRI |null |auto_increment |
student_name |varchar(45) |NO | |null | |
student_dob |datetime |NO |PRI |null | |
student_address |varchar(45) |NO | |null | |
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Location of eclipse.ini file on Mac OS X - Mac-OS-X
- Display full website URL/address in Safari macOS Browser - MacOS
- Fix: Error: Could not find or load main class org.gradle.wrapper. GradleWrapperMain - Gradle
- How to Generate SHA-256 Hash in Java With Example - Java
- Steps to Delete a SharePoint Site - SharePoint
- How to remove username from Mac Menu Bar? - MacOS
- MySQL Error :1006 SQLSTATE: HY000 (ER_CANT_CREATE_DB) Message: Can't create database 'mydb' (errno: 28) - MySQL
- How to extract Java Jar/War/Ear files in Linux - Java