Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum types {
- Oracle,MySQL
- }
- public class Iconnection {
- types contype;
- public String username;
- public String password;
- public String driver;
- public String DB_URL;
- public Connection connection;
- public Statement statement;
- public Iconnection(types contype)
- {
- this.contype=contype;
- }
- public types gettype()
- {
- return contype;
- }
- public void settype(types contype)
- {
- this.contype=contype;
- }
- protected void construct(String driver,String username,String password)
- {
- try {
- Class.forName(driver);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(ORcon.class.getName()).log(Level.SEVERE, null, ex);
- System.out.println("can't connection1");
- }
- try {
- this.connection=DriverManager.getConnection(driver, username, password);
- } catch (SQLException ex) {
- Logger.getLogger(ORcon.class.getName()).log(Level.SEVERE, null, ex);
- System.out.println("can't connection2");
- }
- try {
- this.statement=this.connection.createStatement();
- } catch (SQLException ex) {
- Logger.getLogger(ORcon.class.getName()).log(Level.SEVERE, null, ex);
- System.out.println("can't connection3");
- }
- }
- }
- public class ORcon extends Iconnection{
- private static Iconnection instance = new Iconnection(types.Oracle);
- ORcon()
- {
- super(types.Oracle);
- this.username = "root";
- this.password="root";
- this.driver="oracle.jdbc.driver.OracleDriver";
- this.DB_URL="jdbc:oracle:thin:@localhost:1521:mydb";
- super.construct(this.driver,this.username,this.password);
- }
- public static Iconnection getinstance()
- {
- if(instance==null)
- instance=new ORcon();
- return instance;
- }
- }
- public class MYScon extends Iconnection{
- private static Iconnection instance = new Iconnection(types.Oracle);
- MYScon()
- {
- super(types.MySQL);
- this.username = "root";
- this.password="root";
- this.driver="com.mysql.jdbc.Driver";
- this.DB_URL="jdbc:mysql://localhost:3306/mydb";
- super.construct(this.driver,this.username,this.password);
- }
- public static Iconnection getinstance()
- {
- if(instance==null)
- instance=new MYScon();
- return instance;
- }
- }
- public class ConnectionFactory {
- public static Iconnection creatconnectoin(types cotype)
- {
- Iconnection Icon=null;
- switch (cotype)
- {
- case Oracle:
- Icon=new ORcon();
- break;
- case MySQL:
- Icon=new MYScon();
- break;
- }
- return Icon;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement