Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbc;
- import java.sql.Connection;
- import java.sql.Driver;
- import java.sql.DriverManager;
- import java.sql.DriverPropertyInfo;
- import java.sql.SQLException;
- import java.sql.SQLFeatureNotSupportedException;
- import java.util.Properties;
- import java.util.logging.Logger;
- import dbms.DBMS;
- public class JDriver implements Driver {
- private String protocolName;
- private String dbName;
- private String userName;
- private String password;
- // Constructor and registering the driver through the driver manager
- public JDriver() {
- protocolName = "";
- dbName = "";
- userName="";
- password="";
- try {
- DriverManager.registerDriver(new JDriver());
- } catch (SQLException e) {
- // print can not register driver.
- }
- }
- @Override
- public boolean acceptsURL(String url) throws SQLException {
- // TODO Auto-generated method stub
- if (checkURL(url))
- return true;
- return false;
- }
- @Override
- public Connection connect(String url, Properties info) throws SQLException {
- userName=info.getProperty("userName");
- password=info.getProperty("Password");
- //intialize a new dbms per connection
- DBMS dbms=new DBMS(protocolName,userName,password);
- /*try{
- dbms=new DBMS();
- }
- catch(Exception e){
- //exception thrown when any thing happen in dbms.
- }*/
- ///validate user name and password from configuration file
- JConnection connection=new JConnection(dbms);
- return connection;
- }
- @Override
- public int getMajorVersion() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public int getMinorVersion() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public Logger getParentLogger() throws SQLFeatureNotSupportedException {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
- throws SQLException {
- // TODO Auto-generated method stub
- DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[1];
- DriverPropertyInfo property;
- property = new DriverPropertyInfo(userName, password);
- propertyInfo[0] = property;
- return propertyInfo;
- }
- @Override
- public boolean jdbcCompliant() {
- // TODO Auto-generated method stub
- return false;
- }
- private boolean checkURL(String url) {
- String[] urlInfo = url.split(":");
- if (urlInfo.length != 3) {
- return false;
- } else {
- if (!urlInfo[0].equals("jdbc")) {
- return false;
- }
- if (!urlInfo[1].equals("xmldb") && !urlInfo[1].equals("altdb")) {
- return false;
- } else
- protocolName = urlInfo[1];
- char[] localHost = urlInfo[2].toCharArray();
- if (localHost[0] != '/' || localHost[1] != '/') {
- return false;
- }
- // urlInfo[2].replaceAll("[^a-zA-Z]", "");//it doesnot replace :(
- dbName = urlInfo[2];
- /*for (int i = 0; i < urlInfo.length; i++) {
- System.out.println(urlInfo[i]);
- }*/
- return true;
- }
- }
- public static void main(final String[] args) {
- JDriver driver = new JDriver();
- System.out.println(driver.checkURL("jdbc:xmldb://localhost"));
- }
- }
Add Comment
Please, Sign In to add comment