Database Query designed using a simple factory method (implemented using Java code)

Source: Internet
Author: User

There are six classes in total. The main database class is dbconn. Java, the sub-classes are dbquery. Java and dbupdate. java. The factory classes are dbstore. Java, dbquerystore. Java, and dbupdatestore. java.

 

Dbconn. Java code:

  1. Package com. Test. dB;
  2. Import com. Test. util. basedatesource;
  3. Import javax. SQL. datasource;
  4. Import javax. Naming. context;
  5. Import javax. Naming. initialcontext;
  6. Import javax. Naming. namingexception;
  7. Import java. SQL. connection;
  8. Import java. SQL. statement;
  9. Import java. SQL. preparedstatement;
  10. Import java. SQL. resultset;
  11. Import java. SQL. sqlexception;
  12. Public class dbconn {
  13. // Declare database connection, statement, and preparedstatement
  14. Connection con;
  15. Statement stmt;
  16. Preparedstatement psmt;
  17. // Obtain the database connection
  18. Public void getconn () throws namingexception, sqlexception {
  19. Context CTX = new initialcontext ();
  20. String strlookup = basedatesource. judi_ SQL;
  21. Datasource DS = (datasource) CTX. Lookup (strlookup );
  22. Con = Ds. getconnection ();
  23. System. Out. println ("database connection enabled ");
  24. }
  25. // Process the SQL statement of the database. The empty method is used here. The subclass overrides this method.
  26. Public void sqlprocess (string SQL ){}
  27. // Process the SQL statement of the database. The empty method is used here. The subclass rewrites the method.
  28. Public void sqlprocess (string username, string password ){}
  29. Public resultset getrs (){
  30. Return NULL;
  31. }
  32. // Close the database connection
  33. Public void closecon () throws sqlexception {
  34. Con. Close ();
  35. System. Out. println ("Database Connection closed ");
  36. }
  37. }

Dbquery. Java code:

  1. Package com. Test. dB;
  2. Import java. SQL. resultset;
  3. Import java. SQL. sqlexception;
  4. Public class dbquery
  5. Extends dbconn {
  6. Private resultset RS;
  7. // This inherits the abstract method of sqlprocess from dbconn. java.
  8. Public void sqlprocess (string SQL ){
  9. Try {
  10. Stmt = con. createstatement ();
  11. Rs = stmt.exe cutequery (SQL );
  12. System. Out. println ("database queried successfully" + SQL );
  13. }
  14. Catch (sqlexception e ){
  15. E. printstacktrace ();
  16. }
  17. }
  18. Public resultset getrs (){
  19. System. Out. println ("returned result set ");
  20. Return Rs;
  21. }
  22. Public void closecon () throws sqlexception {
  23. Rs. Close ();
  24. Con. Close ();
  25. System. Out. println ("the result set and database connection are closed ");
  26. }
  27. }

Dbupdate. Java code:

  1. Package com. Test. dB;
  2. Import java. SQL .*;
  3. Public class dbupdate
  4. Extends dbconn {
  5. // This inherits the abstract method of sqlprocess from dbconn. java.
  6. Public void sqlprocess (string SQL ){
  7. Try {
  8. Stmt = con. createstatement ();
  9. Stmt.exe cuteupdate (SQL );
  10. System. Out. println ("database updated successfully" + SQL );
  11. }
  12. Catch (sqlexception e ){
  13. E. printstacktrace ();
  14. }
  15. }
  16. }

Dbstore. Java code:

  1. Package com. Test. dbstore;
  2. Import com. Test. DB. dbconn;
  3. Import javax. Naming. namingexception;
  4. Import java. SQL. sqlexception;
  5. Public abstract class dbstore {
  6. // Declare the database
  7. Dbconn dBc;
  8. // Query method of the database to be used for ordering
  9. Public dbconn Order (string SQL) throws namingexception, sqlexception {
  10. // It is a subclass of DBC new
  11. DBC = create ();
  12. // Create connections and create connections in a unified manner.
  13. DBC. getconn ();
  14. // Process the SQL statement here and process it automatically
  15. DBC. sqlprocess (SQL );
  16. Return dBc;
  17. }
  18. Public dbconn Order (string username, string password) throws namingexception,
  19. Sqlexception {
  20. // It is a subclass of DBC new
  21. DBC = create ();
  22. // Create connections and create connections in a unified manner.
  23. DBC. getconn ();
  24. // Process the SQL statement here and process it automatically
  25. DBC. sqlprocess (username, password );
  26. Return dBc;
  27. }
  28. // Create the database query method to be used. The subclass inherits the secondary abstract method to return the new subclass.
  29. Abstract dbconn create ();
  30. }

Dbquerystore. Java code:

  1. Package com. Test. dbstore;
  2. Import com. Test. DB .*;
  3. Public class dbquerystore
  4. Extends dbstore {
  5. // Inherit the abstract method here
  6. Dbconn create (){
  7. Return new dbquery ();
  8. }
  9. }

Dbupdatestore. Java code:

  1. Package com. Test. dbstore;
  2. Import com. Test. DB. dbconn;
  3. Import com. Test. DB. dbupdate;
  4. Public class dbupdatestore
  5. Extends dbstore {
  6. // Inherit the abstract method here
  7. Dbconn create (){
  8. Return new dbupdate ();
  9. }
  10. }

Use the following code to call a database:

Dbstore DS = new dbupdatestore ();
Dbconn Dc = Ds. Order (SQL );

DC. closeconn ();

 

Dbstore DS = new dbquerystore ();
Dbconn Dc = Ds. Order (SQL );

Resultset rs = Dc. getrs ()

DC. closeconn ();

 

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.