Guest User

JDriver

a guest
Dec 11th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. package jdbc;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Driver;
  5. import java.sql.DriverManager;
  6. import java.sql.DriverPropertyInfo;
  7. import java.sql.SQLException;
  8. import java.sql.SQLFeatureNotSupportedException;
  9. import java.util.Properties;
  10. import java.util.logging.Logger;
  11.  
  12. import dbms.DBMS;
  13.  
  14. public class JDriver implements Driver {
  15.  
  16. private String protocolName;
  17. private String dbName;
  18. private String userName;
  19. private String password;
  20.  
  21. // Constructor and registering the driver through the driver manager
  22. public JDriver() {
  23. protocolName = "";
  24. dbName = "";
  25. userName="";
  26. password="";
  27. try {
  28. DriverManager.registerDriver(new JDriver());
  29. } catch (SQLException e) {
  30. // print can not register driver.
  31. }
  32. }
  33.  
  34. @Override
  35. public boolean acceptsURL(String url) throws SQLException {
  36. // TODO Auto-generated method stub
  37. if (checkURL(url))
  38. return true;
  39.  
  40. return false;
  41. }
  42.  
  43. @Override
  44. public Connection connect(String url, Properties info) throws SQLException {
  45. userName=info.getProperty("userName");
  46. password=info.getProperty("Password");
  47. //intialize a new dbms per connection
  48. DBMS dbms=new DBMS(protocolName,userName,password);
  49. /*try{
  50. dbms=new DBMS();
  51. }
  52. catch(Exception e){
  53. //exception thrown when any thing happen in dbms.
  54. }*/
  55. ///validate user name and password from configuration file
  56. JConnection connection=new JConnection(dbms);
  57. return connection;
  58. }
  59.  
  60. @Override
  61. public int getMajorVersion() {
  62. // TODO Auto-generated method stub
  63. return 0;
  64. }
  65.  
  66. @Override
  67. public int getMinorVersion() {
  68. // TODO Auto-generated method stub
  69. return 0;
  70. }
  71.  
  72. @Override
  73. public Logger getParentLogger() throws SQLFeatureNotSupportedException {
  74. // TODO Auto-generated method stub
  75. return null;
  76. }
  77.  
  78. @Override
  79. public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
  80. throws SQLException {
  81. // TODO Auto-generated method stub
  82. DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[1];
  83. DriverPropertyInfo property;
  84. property = new DriverPropertyInfo(userName, password);
  85. propertyInfo[0] = property;
  86. return propertyInfo;
  87. }
  88.  
  89. @Override
  90. public boolean jdbcCompliant() {
  91. // TODO Auto-generated method stub
  92. return false;
  93. }
  94.  
  95. private boolean checkURL(String url) {
  96. String[] urlInfo = url.split(":");
  97. if (urlInfo.length != 3) {
  98. return false;
  99. } else {
  100. if (!urlInfo[0].equals("jdbc")) {
  101. return false;
  102. }
  103. if (!urlInfo[1].equals("xmldb") && !urlInfo[1].equals("altdb")) {
  104. return false;
  105. } else
  106. protocolName = urlInfo[1];
  107. char[] localHost = urlInfo[2].toCharArray();
  108. if (localHost[0] != '/' || localHost[1] != '/') {
  109. return false;
  110. }
  111. // urlInfo[2].replaceAll("[^a-zA-Z]", "");//it doesnot replace :(
  112. dbName = urlInfo[2];
  113. /*for (int i = 0; i < urlInfo.length; i++) {
  114. System.out.println(urlInfo[i]);
  115. }*/
  116. return true;
  117. }
  118. }
  119.  
  120. public static void main(final String[] args) {
  121. JDriver driver = new JDriver();
  122. System.out.println(driver.checkURL("jdbc:xmldb://localhost"));
  123. }
  124. }
Add Comment
Please, Sign In to add comment