//Get data table structure
Public static void Getconnandtablestruct () {
Connection Connection = null;
PreparedStatement pstmt = null;
ResultSetMetaData rsmd = null;
try {
//mysql Connection
class.forname ("Org.gjt.mm.mysql.Driver");
connection = drivermanager.getconnection (
"Jdbc:mysql://localhost:3306/test", "root", "root");
//oracle Connection
//Class.forName ("Oracle.jdbc.driver.OracleDriver");
//String url= "Jdbc:oracle:thin:@173.10.2.11:1521:test";
//connection=drivermanager.getconnection (URL, "root", "root");
pstmt = (preparedstatement) connection.preparestatement ("SELECT * from user");
Pstmt.execute (); This is especially important to note: If you are Oracle, you can use MySQL without adding.
RSMD = (resultsetmetadata) pstmt.getmetadata ();
classname= "User";
tablename= "user";
for (int i = 1; i < Rsmd.getcolumncount () + 1; i++) {
Table Table=new table ();
Table.setcolumnname (Rsmd.getcolumnname (i));
Table.setcolumntype (Rsmd.getcolumnclassname (i). substring (rsmd.getcolumnclassname
(i). LastIndexOf (".") +1));
tables.add (table);
System.out.println (Rsmd.getcolumnname (i) + "" +rsmd.getcolumntypename (i)
+ "" +rsmd.getcolumnclassname (i) + "" +rsmd.gettablename (i));
}
}
catch (ClassNotFoundException cnfex) {
cnfex.printstacktrace ();
}
catch (SQLException Sqlex) {
sqlex.printstacktrace ();
}
}
Note:
The ResultSetMetaData object can be used to find the type and attributes of a column in a ResultSet.
Method Index
getcatalogname (int)
Gets the directory name of the table for the column.
getColumnCount ()
gets the number of columns in the ResultSet.
getcolumndisplaysize (int)
gets the normal maximum character width for the column.
getcolumnlabel (int)
gets the printed output and the suggested column headings for display.
getcolumnname (int)
gets the column name.
getcolumntype (int)
gets the SQL type of a column.
getcolumntypename (int)
gets the data source-specific type name for a column.
getprecision (int)
gets the number of decimal digits of a column.
getscale (int)
gets the number of digits to the right of the decimal point of a column.
getschemaname (int)
gets the schema of the table for a column.
gettablename (int)
Gets the table name of the column.
isautoincrement (int)
whether the column is automatically counted, so it is read-only.
iscasesensitive (int)
whether the column is case-sensitive.
iscurrency (int)
whether the column is generic.
isdefinitelywritable (int)
whether the write operation to the column must be successful.
isnullable (int)
whether a NULL value can be placed in the column.
isreadonly (int)
whether the column is not writable.
issearchable (int)
whether the column can be queried.
issigned (int)
whether the column is a signed number.
iswritable (int)
whether the write operation to the column will succeed.