如何只用4步,实现一个自定义JDBC驱动?

Posted 麒思妙想

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何只用4步,实现一个自定义JDBC驱动?相关的知识,希望对你有一定的参考价值。

如何只用4步,实现一个自定义JDBC驱动? 那么今天就让我们尝试来完成一个csv-jdbc驱动,并完成简单查询。

JqDriver.java

首先创建驱动类,这里将 jdbc url 里的路径截取出来,在创建 connection 的时候传递进去。

package com.dafei1288.jimsql.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;

public class JqDriver implements Driver 

  private static final JqDriver INSTANCE = new JqDriver();
  private static final String DEFAULT_URL = "jdbc:jimsql:";
  private static String DATADIR = "/tmp";
  private static final ThreadLocal<Connection> DEFAULT_CONNECTION =
      new ThreadLocal<>();

  private static boolean registered;

  static 
    load();
  

  private static JqDriver load() 
    try 
      if (!registered) 
        registered = true;
        DriverManager.registerDriver(INSTANCE);
      
     catch (SQLException e) 
      e.printStackTrace();
    
    return INSTANCE;
  

  @Override
  public Connection connect(String url, Properties info) throws SQLException 
    System.out.println(url);
    System.out.println(info);
    String filepath = url.replace(DEFAULT_URL,"");
    System.out.println(filepath);
    return new JqConnection(filepath);
  

  @Override
  public boolean acceptsURL(String url) throws SQLException 
    System.out.println("acceptsURL ? "+url);
    return false;
  

  @Override
  public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException 
    return new DriverPropertyInfo[0];
  

  @Override
  public int getMajorVersion() 
    return 0;
  

  @Override
  public int getMinorVersion() 
    return 0;
  

  @Override
  public boolean jdbcCompliant() 
    return false;
  

  @Override
  public Logger getParentLogger() throws SQLFeatureNotSupportedException 
    return null;
  


JqConnection.java

接下来我们要实现connection类,接受文本库的基础路径,并实现创建 createStatement方法

package com.dafei1288.jimsql.jdbc;

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;
import java.util.concurrent.Executor;

public class JqConnection implements Connection 

  private String datapath;
  JqConnection(String datapath)
    this.datapath = datapath;
  

  @Override
  public Statement createStatement() throws SQLException 
    return new JqStatement(datapath);
  

  @Override
  public PreparedStatement prepareStatement(String sql) throws SQLException 
    return null;
  

  @Override
  public CallableStatement prepareCall(String sql) throws SQLException 
    return null;
  

  @Override
  public String nativeSQL(String sql) throws SQLException 
    return null;
  

  @Override
  public void setAutoCommit(boolean autoCommit) throws SQLException 

  

  @Override
  public boolean getAutoCommit() throws SQLException 
    return false;
  

  @Override
  public void commit() throws SQLException 

  

  @Override
  public void rollback() throws SQLException 

  

  @Override
  public void close() throws SQLException 

  

  @Override
  public boolean isClosed() throws SQLException 
    return false;
  

  @Override
  public DatabaseMetaData getMetaData() throws SQLException 
    return null;
  

  @Override
  public void setReadOnly(boolean readOnly) throws SQLException 

  

  @Override
  public boolean isReadOnly() throws SQLException 
    return false;
  

  @Override
  public void setCatalog(String catalog) throws SQLException 

  

  @Override
  public String getCatalog() throws SQLException 
    return null;
  

  @Override
  public void setTransactionIsolation(int level) throws SQLException 

  

  @Override
  public int getTransactionIsolation() throws SQLException 
    return 0;
  

  @Override
  public SQLWarning getWarnings() throws SQLException 
    return null;
  

  @Override
  public void clearWarnings() throws SQLException 

  

  @Override
  public Statement createStatement(int resultSetType, int resultSetConcurrency)
      throws SQLException 
    return null;
  

  @Override
  public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
      throws SQLException 
    return null;
  

  @Override
  public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
      throws SQLException 
    return null;
  

  @Override
  public Map<String, Class<?>> getTypeMap() throws SQLException 
    return null;
  

  @Override
  public void setTypeMap(Map<String, Class<?>> map) throws SQLException 

  

  @Override
  public void setHoldability(int holdability) throws SQLException 

  

  @Override
  public int getHoldability() throws SQLException 
    return 0;
  

  @Override
  public Savepoint setSavepoint() throws SQLException 
    return null;
  

  @Override
  public Savepoint setSavepoint(String name) throws SQLException 
    return null;
  

  @Override
  public void rollback(Savepoint savepoint) throws SQLException 

  

  @Override
  public void releaseSavepoint(Savepoint savepoint) throws SQLException 

  

  @Override
  public Statement createStatement(int resultSetType, int resultSetConcurrency,
      int resultSetHoldability) throws SQLException 
    return null;
  

  @Override
  public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
      int resultSetHoldability) throws SQLException 
    return null;
  

  @Override
  public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
      int resultSetHoldability) throws SQLException 
    return null;
  

  @Override
  public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException 
    return null;
  

  @Override
  public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException 
    return null;
  

  @Override
  public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException 
    return null;
  

  @Override
  public Clob createClob() throws SQLException 
    return null;
  

  @Override
  public Blob createBlob() throws SQLException 
    return null;
  

  @Override
  public NClob createNClob() throws SQLException 
    return null;
  

  @Override
  public SQLXML createSQLXML() throws SQLException 
    return null;
  

  @Override
  public boolean isValid(int timeout) throws SQLException 
    return false;
  

  @Override
  public void setClientInfo(String name, String value) throws SQLClientInfoException 

  

  @Override
  public void setClientInfo(Properties properties) throws SQLClientInfoException 

  

  @Override
  public String getClientInfo(String name) throws SQLException 
    return null;
  

  @Override
  public Properties getClientInfo() throws SQLException 
    return null;
  

  @Override
  public Array createArrayOf(String typeName, Object[] elements) throws SQLException 
    return null;
  

  @Override
  public Struct createStruct(String typeName, Object[] attributes) throws SQLException 
    return null;
  

  @Override
  public void setSchema(String schema) throws SQLException 

  

  @Override
  public String getSchema() throws SQLException 
    return null;
  

  @Override
  public void abort(Executor executor) throws SQLException 

  

  @Override
  public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException 

  

  @Override
  public int getNetworkTimeout() throws SQLException 
    return 0;
  

  @Override
  public <T> T unwrap(Class<T> iface) throws SQLException 
    return null;
  

  @Override
  public boolean isWrapperFor(Class<?> iface) throws SQLException 
    return false;
  


JqStatement.java

接下来,我们实现statement类,接受connection传递下来的数据库路径,实现查询方法 executeQuery, 接收到 sql以后,进行简单分析,将表名,列名,以及数据库路径传递给 resultset

package com.dafei1288.jimsql.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JqStatement implements Statement 

  private String datapath;
  JqStatement(String datapath)
    this.datapath = datapath;
  

  @Override
  public ResultSet executeQuery(String sql) throws SQLException 

    Pattern pattern = Pattern.compile("^select (\\\\*|[a-z0-9,]*) from ([a-z]+)");
    Matcher matcher = pattern.matcher(sql);
    String tableName = "";
    List<String> cols = null;
    if(matcher.find())
      String cs = matcher.group(1);
      if(cs.equals("*"))
      else
        String[] cns =cs.split(",");
        cols = Arrays.stream(cns).toList();
      
      tableName = matcher.group(2);
    
    JqResultSet jqResultSet = new JqResultSet(datapath,cols,tableName);

    return jqResultSet;
  

  @Override
  public int executeUpdate(String sql) throws SQLException 
    return 0;
  

  @Override
  public void close(

以上是关于如何只用4步,实现一个自定义JDBC驱动?的主要内容,如果未能解决你的问题,请参考以下文章

如何只用4步,实现一个自定义JDBC驱动?

如何只用4步,实现一个自定义JDBC驱动?

如何使用 JDBC 驱动程序为 django 编写自定义数据库适配器?

创建驱动程序定义时如何添加 mySQL JDBC 驱动程序?

JDBC

java JDBC 基本步骤