Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. public enum types {
  2. Oracle,MySQL
  3. }
  4.  
  5. public class Iconnection {
  6. types contype;
  7. public String username;
  8. public String password;
  9. public String driver;
  10. public String DB_URL;
  11. public Connection connection;
  12. public Statement statement;
  13. public Iconnection(types contype)
  14. {
  15. this.contype=contype;
  16.  
  17. }
  18. public types gettype()
  19. {
  20. return contype;
  21. }
  22. public void settype(types contype)
  23. {
  24. this.contype=contype;
  25. }
  26.  
  27. protected void construct(String driver,String username,String password)
  28. {
  29. try {
  30. Class.forName(driver);
  31. } catch (ClassNotFoundException ex) {
  32. Logger.getLogger(ORcon.class.getName()).log(Level.SEVERE, null, ex);
  33. System.out.println("can't connection1");
  34. }
  35. try {
  36. this.connection=DriverManager.getConnection(driver, username, password);
  37. } catch (SQLException ex) {
  38. Logger.getLogger(ORcon.class.getName()).log(Level.SEVERE, null, ex);
  39. System.out.println("can't connection2");
  40. }
  41. try {
  42. this.statement=this.connection.createStatement();
  43. } catch (SQLException ex) {
  44. Logger.getLogger(ORcon.class.getName()).log(Level.SEVERE, null, ex);
  45. System.out.println("can't connection3");
  46. }
  47.  
  48. }
  49. }
  50.  
  51. public class ORcon extends Iconnection{
  52. private static Iconnection instance = new Iconnection(types.Oracle);
  53. ORcon()
  54. {
  55. super(types.Oracle);
  56. this.username = "root";
  57. this.password="root";
  58. this.driver="oracle.jdbc.driver.OracleDriver";
  59. this.DB_URL="jdbc:oracle:thin:@localhost:1521:mydb";
  60. super.construct(this.driver,this.username,this.password);
  61. }
  62. public static Iconnection getinstance()
  63. {
  64. if(instance==null)
  65. instance=new ORcon();
  66. return instance;
  67. }
  68. }
  69.  
  70. public class MYScon extends Iconnection{
  71. private static Iconnection instance = new Iconnection(types.Oracle);
  72. MYScon()
  73. {
  74. super(types.MySQL);
  75. this.username = "root";
  76. this.password="root";
  77. this.driver="com.mysql.jdbc.Driver";
  78. this.DB_URL="jdbc:mysql://localhost:3306/mydb";
  79. super.construct(this.driver,this.username,this.password);
  80. }
  81. public static Iconnection getinstance()
  82. {
  83. if(instance==null)
  84. instance=new MYScon();
  85. return instance;
  86. }
  87. }
  88.  
  89. public class ConnectionFactory {
  90. public static Iconnection creatconnectoin(types cotype)
  91. {
  92. Iconnection Icon=null;
  93. switch (cotype)
  94. {
  95. case Oracle:
  96. Icon=new ORcon();
  97. break;
  98.  
  99. case MySQL:
  100. Icon=new MYScon();
  101. break;
  102. }
  103. return Icon;
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement