
how to get information about database like, what are the names of tables that database includes ? what are the names of fields in corresponding tables ? in mysql database on linux environment

Here is a code that fetches the information from the database and display it on the console. The given code retrieve table names and their fields from the particular database.
import java.sql.*;
public class AccessDatabases{
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
DatabaseMetaData meta = con.getMetaData();
ResultSet rs = meta.getTables(null, null, "%", null);
String tableNames = "";
while (rs.next()) {
tableNames = rs.getString(3);
System.out.println("Table Name: "+tableNames);
ResultSet rst = st.executeQuery("SELECT * FROM "+tableNames+"");
ResultSetMetaData md = rst.getMetaData();
int col = md.getColumnCount();
System.out.println("Number of Column : "+ col);
System.out.println("Columns Name: ");
for (int i = 1; i <= col; i++){
String col_name = md.getColumnName(i);
System.out.println(col_name);
}
}
}
catch (Exception e) {
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.