Description of Database Table : Table « Database « Java Tutorial






import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class Main {
  public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial",
        "root", "root");

    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("DESCRIBE myTable");
    ResultSetMetaData md = rs.getMetaData();
    int col = md.getColumnCount();
    for (int i = 1; i <= col; i++) {
      String col_name = md.getColumnName(i);
      System.out.println(col_name);
    }
    DatabaseMetaData dbm = con.getMetaData();
    ResultSet rs1 = dbm.getColumns(null, "%", "myTable", "%");
    while (rs1.next()) {
      String col_name = rs1.getString("COLUMN_NAME");
      String data_type = rs1.getString("TYPE_NAME");
      int data_size = rs1.getInt("COLUMN_SIZE");
      int nullable = rs1.getInt("NULLABLE");
      System.out.println(col_name + " " + data_type + "(" + data_size + ")");
      if (nullable == 1) {
        System.out.println("YES");
      } else {
        System.out.println("NO");
      }
    }
  }
}








20.26.Table
20.26.1.Create a table in database
20.26.2.Delete record from table
20.26.3.Description of Database Table
20.26.4.Adding a New Column Name in Database Table
20.26.5.Change Column Name of a Table
20.26.6.Make Unique Column in Database Table
20.26.7.Remove Unique Column in Database Table
20.26.8.Arrange a Column of Database Table
20.26.9.Sum of Column in a Database Table
20.26.10.Delete a Column from a Database Table
20.26.11.Copy data from one table to another in a database
20.26.12.Drop table from database
20.26.13.Copy One Database Table to Another