Hi folks,
I was working for creating a jdbc driver for supporting some db's like mysql,oracle along with connection pooling. So thought just sharing how simple a driver creation is in JAVA. The sample is about writing a wrapper around mysql connector library, once we have this we shall have control over the db operations, like changing the db dynamically based some url's etc.
Following are code snippet ,
I was working for creating a jdbc driver for supporting some db's like mysql,oracle along with connection pooling. So thought just sharing how simple a driver creation is in JAVA. The sample is about writing a wrapper around mysql connector library, once we have this we shall have control over the db operations, like changing the db dynamically based some url's etc.
Following are code snippet ,
Driver :
package com.raghu.jdbc;
import com.raghu.jdbc.nativeconnection.NativeConnection;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Properties;
public class GenericDriver implements Driver {
public static GenericDriver driver = null;
static {
try {
driver = new GenericDriver();
DriverManager.registerDriver(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
public Connection connect(String url, Properties info) throws SQLException {
try {
if (!acceptsURL(url)) {
return null;
}
System.out.println("############# connect ################");
System.out.println("############## Properties :" + info);
url = "jdbc:mysql://localhost:3306/";
GenericConnection cw=new GenericConnection(new NativeConnection().getNativeConnection(url,"root", "root"));
return cw;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public boolean acceptsURL(String url) throws SQLException {
if ((url != null) && (url.startsWith("common:JDBC"))) {
return true;
}
return false;
}
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}
public int getMajorVersion() {
throw new UnsupportedOperationException("Not supported yet.");
}
public int getMinorVersion() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean jdbcCompliant() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
import com.raghu.jdbc.nativeconnection.NativeConnection;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Properties;
public class GenericDriver implements Driver {
public static GenericDriver driver = null;
static {
try {
driver = new GenericDriver();
DriverManager.registerDriver(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
public Connection connect(String url, Properties info) throws SQLException {
try {
if (!acceptsURL(url)) {
return null;
}
System.out.println("############# connect ################");
System.out.println("############## Properties :" + info);
url = "jdbc:mysql://localhost:3306/";
GenericConnection cw=new GenericConnection(new NativeConnection().getNativeConnection(url,"root", "root"));
return cw;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public boolean acceptsURL(String url) throws SQLException {
if ((url != null) && (url.startsWith("common:JDBC"))) {
return true;
}
return false;
}
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}
public int getMajorVersion() {
throw new UnsupportedOperationException("Not supported yet.");
}
public int getMinorVersion() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean jdbcCompliant() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Connection :
package com.raghu.jdbc;
import com.raghu.jdbc.nativeconnection.NativeConnection;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
public class GenericConnection implements Connection {
public Connection nativeConnection = null;
public GenericConnection(Connection con) {
nativeConnection = con;
}
public Statement createStatement() throws SQLException {
System.out.println("inside 1----------");
Statement st=nativeConnection.createStatement();
return st;
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
System.out.println("inside 2----------");
nativeConnection=new NativeConnection().getNativeConnection("jdbc:mysql://localhost:3306/","root", "root");
PreparedStatement pst=nativeConnection.prepareStatement(sql);
return pst;
}
public CallableStatement prepareCall(String sql) throws SQLException {
CallableStatement cst=nativeConnection.prepareCall(sql);
return cst;
}
public String nativeSQL(String sql) throws SQLException {
System.out.println("inside 3----------");
return nativeConnection.nativeSQL(sql);
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
nativeConnection.setAutoCommit(autoCommit);
}
public boolean getAutoCommit() throws SQLException {
return nativeConnection.getAutoCommit();
}
public void commit() throws SQLException {
nativeConnection.commit();
}
public void rollback() throws SQLException {
nativeConnection.rollback();
}
public void close() throws SQLException {
nativeConnection.close();
}
public boolean isClosed() throws SQLException {
return nativeConnection.isClosed();
}
public DatabaseMetaData getMetaData() throws SQLException {
return nativeConnection.getMetaData();
}
public void setReadOnly(boolean readOnly) throws SQLException {
nativeConnection.setReadOnly(readOnly);
}
public boolean isReadOnly() throws SQLException {
return nativeConnection.isReadOnly();
}
public void setCatalog(String catalog) throws SQLException {
nativeConnection.setCatalog(catalog);
}
public String getCatalog() throws SQLException {
return nativeConnection.getCatalog();
}
public void setTransactionIsolation(int level) throws SQLException {
nativeConnection.setTransactionIsolation(level);
}
public int getTransactionIsolation() throws SQLException {
return nativeConnection.getTransactionIsolation();
}
public SQLWarning getWarnings() throws SQLException {
return nativeConnection.getWarnings();
}
public void clearWarnings() throws SQLException {
nativeConnection.clearWarnings();
}
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
System.out.println("inside 4----------");
return nativeConnection.createStatement(resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
System.out.println("inside 5----------");
return nativeConnection.prepareStatement(sql, resultSetType,resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return nativeConnection.prepareCall(sql, resultSetType, resultSetConcurrency);
}
public Map<String, Class<?>> getTypeMap() throws SQLException {
return nativeConnection.getTypeMap();
}
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
nativeConnection.setTypeMap(map);
}
public void setHoldability(int holdability) throws SQLException {
nativeConnection.setHoldability(holdability);
}
public int getHoldability() throws SQLException {
return nativeConnection.getHoldability();
}
public Savepoint setSavepoint() throws SQLException {
return nativeConnection.setSavepoint();
}
public Savepoint setSavepoint(String name) throws SQLException {
return nativeConnection.setSavepoint(name);
}
public void rollback(Savepoint savepoint) throws SQLException {
nativeConnection.rollback(savepoint);
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
nativeConnection.releaseSavepoint(savepoint);
}
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
System.out.println("inside 6----------");
return nativeConnection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
System.out.println("inside 7----------");
return nativeConnection.prepareCall(sql, resultSetType, resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return nativeConnection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
System.out.println("inside 8----------");
return nativeConnection.prepareStatement(sql, autoGeneratedKeys);
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
System.out.println("inside 9----------");
return nativeConnection.prepareStatement(sql, columnIndexes);
}
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
System.out.println("inside 10----------");
return nativeConnection.prepareStatement(sql, columnNames);
}
public Clob createClob() throws SQLException {
return nativeConnection.createClob();
}
public Blob createBlob() throws SQLException {
return nativeConnection.createBlob();
}
public NClob createNClob() throws SQLException {
return nativeConnection.createNClob();
}
public SQLXML createSQLXML() throws SQLException {
return nativeConnection.createSQLXML();
}
public boolean isValid(int timeout) throws SQLException {
return nativeConnection.isValid(timeout);
}
public void setClientInfo(String name, String value) throws SQLClientInfoException {
nativeConnection.setClientInfo(name,value);
}
public void setClientInfo(Properties properties) throws SQLClientInfoException {
nativeConnection.setClientInfo(properties);
}
public String getClientInfo(String name) throws SQLException {
return nativeConnection.getClientInfo(name);
}
public Properties getClientInfo() throws SQLException {
return nativeConnection.getClientInfo();
}
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
return nativeConnection.createArrayOf(typeName, elements);
}
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return nativeConnection.createStruct(typeName, attributes);
}
public <T> T unwrap(Class<T> iface) throws SQLException {
return nativeConnection.unwrap(iface);
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return nativeConnection.isWrapperFor(iface);
}
}
import com.raghu.jdbc.nativeconnection.NativeConnection;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
public class GenericConnection implements Connection {
public Connection nativeConnection = null;
public GenericConnection(Connection con) {
nativeConnection = con;
}
public Statement createStatement() throws SQLException {
System.out.println("inside 1----------");
Statement st=nativeConnection.createStatement();
return st;
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
System.out.println("inside 2----------");
nativeConnection=new NativeConnection().getNativeConnection("jdbc:mysql://localhost:3306/","root", "root");
PreparedStatement pst=nativeConnection.prepareStatement(sql);
return pst;
}
public CallableStatement prepareCall(String sql) throws SQLException {
CallableStatement cst=nativeConnection.prepareCall(sql);
return cst;
}
public String nativeSQL(String sql) throws SQLException {
System.out.println("inside 3----------");
return nativeConnection.nativeSQL(sql);
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
nativeConnection.setAutoCommit(autoCommit);
}
public boolean getAutoCommit() throws SQLException {
return nativeConnection.getAutoCommit();
}
public void commit() throws SQLException {
nativeConnection.commit();
}
public void rollback() throws SQLException {
nativeConnection.rollback();
}
public void close() throws SQLException {
nativeConnection.close();
}
public boolean isClosed() throws SQLException {
return nativeConnection.isClosed();
}
public DatabaseMetaData getMetaData() throws SQLException {
return nativeConnection.getMetaData();
}
public void setReadOnly(boolean readOnly) throws SQLException {
nativeConnection.setReadOnly(readOnly);
}
public boolean isReadOnly() throws SQLException {
return nativeConnection.isReadOnly();
}
public void setCatalog(String catalog) throws SQLException {
nativeConnection.setCatalog(catalog);
}
public String getCatalog() throws SQLException {
return nativeConnection.getCatalog();
}
public void setTransactionIsolation(int level) throws SQLException {
nativeConnection.setTransactionIsolation(level);
}
public int getTransactionIsolation() throws SQLException {
return nativeConnection.getTransactionIsolation();
}
public SQLWarning getWarnings() throws SQLException {
return nativeConnection.getWarnings();
}
public void clearWarnings() throws SQLException {
nativeConnection.clearWarnings();
}
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
System.out.println("inside 4----------");
return nativeConnection.createStatement(resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
System.out.println("inside 5----------");
return nativeConnection.prepareStatement(sql, resultSetType,resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return nativeConnection.prepareCall(sql, resultSetType, resultSetConcurrency);
}
public Map<String, Class<?>> getTypeMap() throws SQLException {
return nativeConnection.getTypeMap();
}
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
nativeConnection.setTypeMap(map);
}
public void setHoldability(int holdability) throws SQLException {
nativeConnection.setHoldability(holdability);
}
public int getHoldability() throws SQLException {
return nativeConnection.getHoldability();
}
public Savepoint setSavepoint() throws SQLException {
return nativeConnection.setSavepoint();
}
public Savepoint setSavepoint(String name) throws SQLException {
return nativeConnection.setSavepoint(name);
}
public void rollback(Savepoint savepoint) throws SQLException {
nativeConnection.rollback(savepoint);
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
nativeConnection.releaseSavepoint(savepoint);
}
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
System.out.println("inside 6----------");
return nativeConnection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
System.out.println("inside 7----------");
return nativeConnection.prepareCall(sql, resultSetType, resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return nativeConnection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
System.out.println("inside 8----------");
return nativeConnection.prepareStatement(sql, autoGeneratedKeys);
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
System.out.println("inside 9----------");
return nativeConnection.prepareStatement(sql, columnIndexes);
}
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
System.out.println("inside 10----------");
return nativeConnection.prepareStatement(sql, columnNames);
}
public Clob createClob() throws SQLException {
return nativeConnection.createClob();
}
public Blob createBlob() throws SQLException {
return nativeConnection.createBlob();
}
public NClob createNClob() throws SQLException {
return nativeConnection.createNClob();
}
public SQLXML createSQLXML() throws SQLException {
return nativeConnection.createSQLXML();
}
public boolean isValid(int timeout) throws SQLException {
return nativeConnection.isValid(timeout);
}
public void setClientInfo(String name, String value) throws SQLClientInfoException {
nativeConnection.setClientInfo(name,value);
}
public void setClientInfo(Properties properties) throws SQLClientInfoException {
nativeConnection.setClientInfo(properties);
}
public String getClientInfo(String name) throws SQLException {
return nativeConnection.getClientInfo(name);
}
public Properties getClientInfo() throws SQLException {
return nativeConnection.getClientInfo();
}
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
return nativeConnection.createArrayOf(typeName, elements);
}
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return nativeConnection.createStruct(typeName, attributes);
}
public <T> T unwrap(Class<T> iface) throws SQLException {
return nativeConnection.unwrap(iface);
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return nativeConnection.isWrapperFor(iface);
}
}
Native connection :
package com.raghu.jdbc.nativeconnection;
import java.sql.ResultSet;
import java.sql.Statement;
public class NativeConnection {
public static String dbflag = "simpledb";
public java.sql.Connection getNativeConnection(String url, String dbuser, String dbpass) {
System.out.println("\n ........getNativeConnection......\n");
java.sql.Connection conn = null;
String driver = "";
try {
driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
url = url + dbflag;
// System.out.println("url.........");
conn = java.sql.DriverManager.getConnection(url, dbuser, dbpass);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("\n ....getNativeConnection(con)......" + conn);
return conn;
}
}
import java.sql.ResultSet;
import java.sql.Statement;
public class NativeConnection {
public static String dbflag = "simpledb";
public java.sql.Connection getNativeConnection(String url, String dbuser, String dbpass) {
System.out.println("\n ........getNativeConnection......\n");
java.sql.Connection conn = null;
String driver = "";
try {
driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
url = url + dbflag;
// System.out.println("url.........");
conn = java.sql.DriverManager.getConnection(url, dbuser, dbpass);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("\n ....getNativeConnection(con)......" + conn);
return conn;
}
}
No comments:
Post a Comment