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:
- Package com. Test. dB;
- Import com. Test. util. basedatesource;
- Import javax. SQL. datasource;
- Import javax. Naming. context;
- Import javax. Naming. initialcontext;
- Import javax. Naming. namingexception;
- Import java. SQL. connection;
- Import java. SQL. statement;
- Import java. SQL. preparedstatement;
- Import java. SQL. resultset;
- Import java. SQL. sqlexception;
- Public class dbconn {
- // Declare database connection, statement, and preparedstatement
- Connection con;
- Statement stmt;
- Preparedstatement psmt;
- // Obtain the database connection
- Public void getconn () throws namingexception, sqlexception {
- Context CTX = new initialcontext ();
- String strlookup = basedatesource. judi_ SQL;
- Datasource DS = (datasource) CTX. Lookup (strlookup );
- Con = Ds. getconnection ();
- System. Out. println ("database connection enabled ");
- }
- // Process the SQL statement of the database. The empty method is used here. The subclass overrides this method.
- Public void sqlprocess (string SQL ){}
- // Process the SQL statement of the database. The empty method is used here. The subclass rewrites the method.
- Public void sqlprocess (string username, string password ){}
- Public resultset getrs (){
- Return NULL;
- }
- // Close the database connection
- Public void closecon () throws sqlexception {
- Con. Close ();
- System. Out. println ("Database Connection closed ");
- }
- }
Dbquery. Java code:
- Package com. Test. dB;
- Import java. SQL. resultset;
- Import java. SQL. sqlexception;
- Public class dbquery
- Extends dbconn {
- Private resultset RS;
- // This inherits the abstract method of sqlprocess from dbconn. java.
- Public void sqlprocess (string SQL ){
- Try {
- Stmt = con. createstatement ();
- Rs = stmt.exe cutequery (SQL );
- System. Out. println ("database queried successfully" + SQL );
- }
- Catch (sqlexception e ){
- E. printstacktrace ();
- }
- }
- Public resultset getrs (){
- System. Out. println ("returned result set ");
- Return Rs;
- }
- Public void closecon () throws sqlexception {
- Rs. Close ();
- Con. Close ();
- System. Out. println ("the result set and database connection are closed ");
- }
- }
Dbupdate. Java code:
- Package com. Test. dB;
- Import java. SQL .*;
- Public class dbupdate
- Extends dbconn {
- // This inherits the abstract method of sqlprocess from dbconn. java.
- Public void sqlprocess (string SQL ){
- Try {
- Stmt = con. createstatement ();
- Stmt.exe cuteupdate (SQL );
- System. Out. println ("database updated successfully" + SQL );
- }
- Catch (sqlexception e ){
- E. printstacktrace ();
- }
- }
- }
Dbstore. Java code:
- Package com. Test. dbstore;
- Import com. Test. DB. dbconn;
- Import javax. Naming. namingexception;
- Import java. SQL. sqlexception;
- Public abstract class dbstore {
- // Declare the database
- Dbconn dBc;
- // Query method of the database to be used for ordering
- Public dbconn Order (string SQL) throws namingexception, sqlexception {
- // It is a subclass of DBC new
- DBC = create ();
- // Create connections and create connections in a unified manner.
- DBC. getconn ();
- // Process the SQL statement here and process it automatically
- DBC. sqlprocess (SQL );
- Return dBc;
- }
- Public dbconn Order (string username, string password) throws namingexception,
- Sqlexception {
- // It is a subclass of DBC new
- DBC = create ();
- // Create connections and create connections in a unified manner.
- DBC. getconn ();
- // Process the SQL statement here and process it automatically
- DBC. sqlprocess (username, password );
- Return dBc;
- }
- // Create the database query method to be used. The subclass inherits the secondary abstract method to return the new subclass.
- Abstract dbconn create ();
- }
Dbquerystore. Java code:
- Package com. Test. dbstore;
- Import com. Test. DB .*;
- Public class dbquerystore
- Extends dbstore {
- // Inherit the abstract method here
- Dbconn create (){
- Return new dbquery ();
- }
- }
Dbupdatestore. Java code:
- Package com. Test. dbstore;
- Import com. Test. DB. dbconn;
- Import com. Test. DB. dbupdate;
- Public class dbupdatestore
- Extends dbstore {
- // Inherit the abstract method here
- Dbconn create (){
- Return new dbupdate ();
- }
- }
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 ();