"JAVA design mode" Bridge connection mode---Jdbc__java

Source: Internet
Author: User

About the role of the JDBC Bridge connection mode for a long time, if the wrong place to hope that the great God can point out, thank you very much .... Be sure to point out that ...




Give me a jdbc little chestnut.


Step one: Establish a bridge-connected interface

Public interface Jdbc_api {public
   void connection ();

Step Two: Create multiple classes (simulate the classes we initiate the connection) implement the interface

public class MySQL implements JDBC_API {public

	void connection () {
		System.out.println ("I want to connect to Mysql");
	}
     

public class Oracle implements JDBC_API {public

	void connection () {
		System.out.println ("I want to connect to Oracle");
	}
     
}
public class DB2 implements JDBC_API {public

	void connection () {
		System.out.println ("I want to connect to DB2");
	}

Step three: Set up abstract class driver (analog manufacturer's connection database), note: Inside to add interface instance object

Public abstract class Driver {
	Jdbc_api JDBC;
   abstract void createfactory (); Create a database vendor
}

Step four: Build a vendor implementation class

public class Mysqldriver extends Driver {

	@Override
	void Createfactory () {
		System.out.println ("I am a MySQL vendor" );
	}

}
public class Oracledriver extends Driver {

	@Override
	void Createfactory () {
		System.out.println (" I am Oracle vendor ");
	}


public class Db2driver extends Driver {

	@Override
	void Createfactory () {
		System.out.println ("I am a DB2 manufacturer") ;
	}

}

Step five: Establish a test connection Drivermanger

public class DriverManager {public
    static void Main (string[] args) {
		Driver db=new oracledriver ();
		Db.createfactory ();
		SYSTEM.OUT.PRINTLN ("------Bridge ODBC-----------");
		Db.jdbc=new Oracle ();
		Db.jdbc.connection ();
		
		SYSTEM.OUT.PRINTLN ("------Note: Bridge connections can reach multiple locations, not specific     oracledriver==>mysql-------");
		Db.jdbc=new Mysql ();
		Db.jdbc.connection ();
	}





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.