Use Java to determine whether a database table is created

Source: Internet
Author: User

  1. Session = hibernatesessionfactory. getsession ();
  2. Connection conn = session. Connection ();
  3. // Determine whether the table risenet_file exists in the Database
  4. Databasemetadata DMD = conn. getmetadata ();
  5. /**
  6. * Databasemetadata. gettablesGettables(StringCatalog,
    StringSchemapattern,
    StringTablenamepattern,
    String[] Types)
    ThrowsSqlexception
  7. * Parameter 1: Table type. I have not found what it means. If it is null, it is all
  8. * Parameter 2: The name of the current user. If this parameter is not specified, all users are queried.
  9. * Parameter 3: Table name, which can be a regular expression,
  10. * Parameter 4: Table type: "table", "View", "system table", "global temporary", "local temporary", "alias ", "synonym ".
  11. The results obtained in resultset include:
    1. Table_catString => table catalog (may benull)
    2. Table_schemString => table Schema (may benull)
    3. Table_nameString => table name
    4. Table_typeString => table type. Typical types are "table", "View", "system table", "global temporary", "local temporary", "alias", "synonym ".
    5. RemarksString => explanatory comment on the table
    6. Type_catString => the types catalog (may benull)
    7. Type_schemString => the types Schema (may benull)
    8. Type_nameString => type name (may benull)
    9. Self_referencing_col_nameString => name of the designated "identifier" column of a typed table (may benull)
    10. Ref_generationString => specifies how values in self_referencing_col_name are created. values are "system", "user", "derived". (may benull)

  12. **/
  13. Resultset rs = DMD. gettables ("", "riseapprove", "risenet_file", (New String [] {"table "}));
  14. // Resultsetmetadata meta = Rs. getmetadata ();
  15. // System. Out. println (meta. getcolumncount ());
  16. // (INT I = 1; I <= meta. getcolumncount (); I ++ ){
  17. // System. Out. println ("---" + meta. getcatalogname (I) + "---");
  18. //}
  19. While (Rs. Next ()){
  20. System. Out. println ("table exists, delete table ");
  21. System. Out. println ("table_cat:" + Rs. getstring ("table_cat "));
  22. System. Out. println ("table_schem:" + Rs. getstring ("table_schem "));
  23. System. Out. println ("table_name:" + Rs. getstring ("table_name "));
  24. System. Out. println ("table_type:" + Rs. getstring ("table_type "));
  25. System. Out. println ("end ");
  26. // Transaction Tx = session. begintransaction ();
  27. // Risefile = newrisefile ();
  28. //// Add data, or use session. saveorupdate (risefile );
  29. //
  30. // Session. Save (risefile );
  31. /// The title will be changed to the second test before submission
  32. //
  33. // List = session. Find ("from risefile where fileguid = '{09a26d19-ffffff-ffff-e0d7-ff400000001 }'");
  34. // If (list! = NULL & list. Size ()> 0 ){
  35. // Risefile file = (risefile) list. Get (0 );
  36. // System. Out. println (file. getappname ());
  37. // System. Out. println (file. gettitile ());
  38. //}
  39. // Session. Delete ("from risefile where fileguid = '{09a26d19-ffff-ffff-e0d7-ff400000001 }'");
  40. //
  41. // Risefile. settitile ("nth test ");
  42. //
  43. // Tx. Commit ();
  44. /// Delete a table
  45. // Operatetable (true );
  46. } // Else {
  47. // Create a table
  48. // System. Out. println ("the table does not exist. Create a table ");
  49. // Operatetable (false );
  50. //}

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.