The realization of the factory method in the actual combat of the big talk design pattern--Link database application

Source: Internet
Author: User
Tags getmessage stmt

The advantages of the factory method here is not to say, online search a lot of yourself to see

If you use the JDBC link database and the database switches from MySQL to Oracle, the change is to switch the name of the driver, and the others do not need to be modified, which is a direct example of factory method flexibility.

Empty talk

On the code:

 Public Abstract classAbstractsqlfactory {//This is an example of a generic class that needs to be controlled in the JDK1.5 feature     Public Abstract<textendsSqlutil> T Createsql (class<t>c); }==================================== PackageCOM.FXR. Application of factory method mode;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.Statement; Public InterfaceSqlutil {//connection to the database     PublicConnection getconnection (); //implementation of methods for shutting down the database     Public voidClosedb (Connection conn, Statement stmt, ResultSet rs); }==================================== PackageCOM.FXR. Application of factory method mode; Public classSqlfactoryextendsabstractsqlfactory{@Override Public<textendsSqlutil> T Createsql (class<t>c) {sqlutil Sqlutil=NULL; Try{sqlutil=(Sqlutil) Class.forName (C.getname ()). Newinstance (); }Catch(Exception e) {System.out.println ("There was an error! "); }        return(T) sqlutil; }}========================================== PackageCOM.FXR. Application of factory method mode;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classMysqlutilimplImplementssqlutil{Private StaticString Driver = "Com.mysql.jdbc.Driver";//database-driven strings    Private StaticString url = "Jdbc:mysql://localhost/studentsystem?useunicode=true&characterencoding=utf-8";//Connection URL    Private StaticString user = "Yonghumingcheng";//Database user name    Private StaticString password = "Mima";//User Password@Override PublicConnection getconnection () {Connection con=NULL; Try{class.forname (driver); }Catch(ClassNotFoundException e) {System.out.println ("Load driver error message:" +e.getmessage ()); }            Try{con=drivermanager.getconnection (Url,user,password); }Catch(SQLException e) {System.out.println ("Database connection Error message:" +e.getmessage ());            E.printstacktrace (); }            returncon; } @Override Public voidClosedb (Connection conn, Statement stmt, ResultSet rs) {//If the result set object is not empty, close the                if(rs! =NULL) {                    Try{rs.close (); } Catch(Exception e) {e.printstacktrace (); }                }                //If the statement object is not empty, close the                if(stmt! =NULL) {                    Try{stmt.close (); } Catch(Exception e) {e.printstacktrace (); }                }                //If the database connection object is not empty, close the                if(Conn! =NULL) {                    Try{conn.close (); } Catch(Exception e) {e.printstacktrace (); }                }            }    }=========================================== PackageCOM.FXR. Application of factory method mode;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classSqlserverutilimplImplementssqlutil{PrivateString Driver = "Com.microsoft.jdbc.sqlserver.SQLServerDriver"; Private Static FinalString uri= "jdbc:sqlserver://127.0.0.1:1433; Databasename=demo.db "; PrivateString userName = "Yonghumingcheng"; PrivateString password = "Mima"; @Override PublicConnection getconnection () {Connection con=NULL; Try{class.forname (driver); }Catch(ClassNotFoundException e) {System.out.println ("Load driver error message:" +e.getmessage ()); }            Try{con=drivermanager.getconnection (Driver,username,password); }Catch(SQLException e) {System.out.println ("Database connection Error message:" +e.getmessage ());            E.printstacktrace (); }            returncon; } @Override Public voidClosedb (Connection conn, Statement stmt, ResultSet rs) {//If the result set object is not empty, close the                if(rs! =NULL) {                    Try{rs.close (); } Catch(Exception e) {e.printstacktrace (); }                }                //If the statement object is not empty, close the                if(stmt! =NULL) {                    Try{stmt.close (); } Catch(Exception e) {e.printstacktrace (); }                }                //If the database connection object is not empty, close the                if(Conn! =NULL) {                    Try{conn.close (); } Catch(Exception e) {e.printstacktrace (); }                }            }}

The realization of the factory method in the actual combat of the big talk design pattern--Link database application

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.