Java database query structure

Source: Internet
Author: User

//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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.