0


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

在这里插入图片描述

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

JqDriver.java

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

packagecom.dafei1288.jimsql.jdbc;importjava.sql.Connection;importjava.sql.Driver;importjava.sql.DriverManager;importjava.sql.DriverPropertyInfo;importjava.sql.SQLException;importjava.sql.SQLFeatureNotSupportedException;importjava.util.Properties;importjava.util.logging.Logger;publicclassJqDriverimplementsDriver{privatestaticfinalJqDriver INSTANCE =newJqDriver();privatestaticfinalString DEFAULT_URL ="jdbc:jimsql:";privatestaticString DATADIR ="/tmp";privatestaticfinalThreadLocal<Connection> DEFAULT_CONNECTION =newThreadLocal<>();privatestaticboolean registered;static{load();}privatestaticJqDriverload(){try{if(!registered){
        registered =true;DriverManager.registerDriver(INSTANCE);}}catch(SQLException e){
      e.printStackTrace();}return INSTANCE;}@OverridepublicConnectionconnect(String url,Properties info)throwsSQLException{System.out.println(url);System.out.println(info);String filepath = url.replace(DEFAULT_URL,"");System.out.println(filepath);returnnewJqConnection(filepath);}@OverridepublicbooleanacceptsURL(String url)throwsSQLException{System.out.println("acceptsURL ? "+url);returnfalse;}@OverridepublicDriverPropertyInfo[]getPropertyInfo(String url,Properties info)throwsSQLException{returnnewDriverPropertyInfo[0];}@OverridepublicintgetMajorVersion(){return0;}@OverridepublicintgetMinorVersion(){return0;}@OverridepublicbooleanjdbcCompliant(){returnfalse;}@OverridepublicLoggergetParentLogger()throwsSQLFeatureNotSupportedException{returnnull;}}

JqConnection.java

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

createStatement

方法

packagecom.dafei1288.jimsql.jdbc;importjava.sql.Array;importjava.sql.Blob;importjava.sql.CallableStatement;importjava.sql.Clob;importjava.sql.Connection;importjava.sql.DatabaseMetaData;importjava.sql.NClob;importjava.sql.PreparedStatement;importjava.sql.SQLClientInfoException;importjava.sql.SQLException;importjava.sql.SQLWarning;importjava.sql.SQLXML;importjava.sql.Savepoint;importjava.sql.Statement;importjava.sql.Struct;importjava.util.Map;importjava.util.Properties;importjava.util.concurrent.Executor;publicclassJqConnectionimplementsConnection{privateString datapath;JqConnection(String datapath){this.datapath = datapath;}@OverridepublicStatementcreateStatement()throwsSQLException{returnnewJqStatement(datapath);}@OverridepublicPreparedStatementprepareStatement(String sql)throwsSQLException{returnnull;}@OverridepublicCallableStatementprepareCall(String sql)throwsSQLException{returnnull;}@OverridepublicStringnativeSQL(String sql)throwsSQLException{returnnull;}@OverridepublicvoidsetAutoCommit(boolean autoCommit)throwsSQLException{}@OverridepublicbooleangetAutoCommit()throwsSQLException{returnfalse;}@Overridepublicvoidcommit()throwsSQLException{}@Overridepublicvoidrollback()throwsSQLException{}@Overridepublicvoidclose()throwsSQLException{}@OverridepublicbooleanisClosed()throwsSQLException{returnfalse;}@OverridepublicDatabaseMetaDatagetMetaData()throwsSQLException{returnnull;}@OverridepublicvoidsetReadOnly(boolean readOnly)throwsSQLException{}@OverridepublicbooleanisReadOnly()throwsSQLException{returnfalse;}@OverridepublicvoidsetCatalog(String catalog)throwsSQLException{}@OverridepublicStringgetCatalog()throwsSQLException{returnnull;}@OverridepublicvoidsetTransactionIsolation(int level)throwsSQLException{}@OverridepublicintgetTransactionIsolation()throwsSQLException{return0;}@OverridepublicSQLWarninggetWarnings()throwsSQLException{returnnull;}@OverridepublicvoidclearWarnings()throwsSQLException{}@OverridepublicStatementcreateStatement(int resultSetType,int resultSetConcurrency)throwsSQLException{returnnull;}@OverridepublicPreparedStatementprepareStatement(String sql,int resultSetType,int resultSetConcurrency)throwsSQLException{returnnull;}@OverridepublicCallableStatementprepareCall(String sql,int resultSetType,int resultSetConcurrency)throwsSQLException{returnnull;}@OverridepublicMap<String,Class<?>>getTypeMap()throwsSQLException{returnnull;}@OverridepublicvoidsetTypeMap(Map<String,Class<?>> map)throwsSQLException{}@OverridepublicvoidsetHoldability(int holdability)throwsSQLException{}@OverridepublicintgetHoldability()throwsSQLException{return0;}@OverridepublicSavepointsetSavepoint()throwsSQLException{returnnull;}@OverridepublicSavepointsetSavepoint(String name)throwsSQLException{returnnull;}@Overridepublicvoidrollback(Savepoint savepoint)throwsSQLException{}@OverridepublicvoidreleaseSavepoint(Savepoint savepoint)throwsSQLException{}@OverridepublicStatementcreateStatement(int resultSetType,int resultSetConcurrency,int resultSetHoldability)throwsSQLException{returnnull;}@OverridepublicPreparedStatementprepareStatement(String sql,int resultSetType,int resultSetConcurrency,int resultSetHoldability)throwsSQLException{returnnull;}@OverridepublicCallableStatementprepareCall(String sql,int resultSetType,int resultSetConcurrency,int resultSetHoldability)throwsSQLException{returnnull;}@OverridepublicPreparedStatementprepareStatement(String sql,int autoGeneratedKeys)throwsSQLException{returnnull;}@OverridepublicPreparedStatementprepareStatement(String sql,int[] columnIndexes)throwsSQLException{returnnull;}@OverridepublicPreparedStatementprepareStatement(String sql,String[] columnNames)throwsSQLException{returnnull;}@OverridepublicClobcreateClob()throwsSQLException{returnnull;}@OverridepublicBlobcreateBlob()throwsSQLException{returnnull;}@OverridepublicNClobcreateNClob()throwsSQLException{returnnull;}@OverridepublicSQLXMLcreateSQLXML()throwsSQLException{returnnull;}@OverridepublicbooleanisValid(int timeout)throwsSQLException{returnfalse;}@OverridepublicvoidsetClientInfo(String name,String value)throwsSQLClientInfoException{}@OverridepublicvoidsetClientInfo(Properties properties)throwsSQLClientInfoException{}@OverridepublicStringgetClientInfo(String name)throwsSQLException{returnnull;}@OverridepublicPropertiesgetClientInfo()throwsSQLException{returnnull;}@OverridepublicArraycreateArrayOf(String typeName,Object[] elements)throwsSQLException{returnnull;}@OverridepublicStructcreateStruct(String typeName,Object[] attributes)throwsSQLException{returnnull;}@OverridepublicvoidsetSchema(String schema)throwsSQLException{}@OverridepublicStringgetSchema()throwsSQLException{returnnull;}@Overridepublicvoidabort(Executor executor)throwsSQLException{}@OverridepublicvoidsetNetworkTimeout(Executor executor,int milliseconds)throwsSQLException{}@OverridepublicintgetNetworkTimeout()throwsSQLException{return0;}@Overridepublic<T>Tunwrap(Class<T> iface)throwsSQLException{returnnull;}@OverridepublicbooleanisWrapperFor(Class<?> iface)throwsSQLException{returnfalse;}}

JqStatement.java

接下来,我们实现statement类,接受connection传递下来的数据库路径,实现查询方法

executeQuery

, 接收到 sql以后,进行简单分析,将表名,列名,以及数据库路径传递给 resultset

packagecom.dafei1288.jimsql.jdbc;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.SQLWarning;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collection;importjava.util.Iterator;importjava.util.List;importjava.util.ListIterator;importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassJqStatementimplementsStatement{privateString datapath;JqStatement(String datapath){this.datapath = datapath;}@OverridepublicResultSetexecuteQuery(String sql)throwsSQLException{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 =newJqResultSet(datapath,cols,tableName);return jqResultSet;}@OverridepublicintexecuteUpdate(String sql)throwsSQLException{return0;}@Overridepublicvoidclose()throwsSQLException{}@OverridepublicintgetMaxFieldSize()throwsSQLException{return0;}@OverridepublicvoidsetMaxFieldSize(int max)throwsSQLException{}@OverridepublicintgetMaxRows()throwsSQLException{return0;}@OverridepublicvoidsetMaxRows(int max)throwsSQLException{}@OverridepublicvoidsetEscapeProcessing(boolean enable)throwsSQLException{}@OverridepublicintgetQueryTimeout()throwsSQLException{return0;}@OverridepublicvoidsetQueryTimeout(int seconds)throwsSQLException{}@Overridepublicvoidcancel()throwsSQLException{}@OverridepublicSQLWarninggetWarnings()throwsSQLException{returnnull;}@OverridepublicvoidclearWarnings()throwsSQLException{}@OverridepublicvoidsetCursorName(String name)throwsSQLException{}@Overridepublicbooleanexecute(String sql)throwsSQLException{returnfalse;}@OverridepublicResultSetgetResultSet()throwsSQLException{returnnull;}@OverridepublicintgetUpdateCount()throwsSQLException{return0;}@OverridepublicbooleangetMoreResults()throwsSQLException{returnfalse;}@OverridepublicvoidsetFetchDirection(int direction)throwsSQLException{}@OverridepublicintgetFetchDirection()throwsSQLException{return0;}@OverridepublicvoidsetFetchSize(int rows)throwsSQLException{}@OverridepublicintgetFetchSize()throwsSQLException{return0;}@OverridepublicintgetResultSetConcurrency()throwsSQLException{return0;}@OverridepublicintgetResultSetType()throwsSQLException{return0;}@OverridepublicvoidaddBatch(String sql)throwsSQLException{}@OverridepublicvoidclearBatch()throwsSQLException{}@Overridepublicint[]executeBatch()throwsSQLException{returnnewint[0];}@OverridepublicConnectiongetConnection()throwsSQLException{returnnull;}@OverridepublicbooleangetMoreResults(int current)throwsSQLException{returnfalse;}@OverridepublicResultSetgetGeneratedKeys()throwsSQLException{returnnull;}@OverridepublicintexecuteUpdate(String sql,int autoGeneratedKeys)throwsSQLException{return0;}@OverridepublicintexecuteUpdate(String sql,int[] columnIndexes)throwsSQLException{return0;}@OverridepublicintexecuteUpdate(String sql,String[] columnNames)throwsSQLException{return0;}@Overridepublicbooleanexecute(String sql,int autoGeneratedKeys)throwsSQLException{returnfalse;}@Overridepublicbooleanexecute(String sql,int[] columnIndexes)throwsSQLException{returnfalse;}@Overridepublicbooleanexecute(String sql,String[] columnNames)throwsSQLException{returnfalse;}@OverridepublicintgetResultSetHoldability()throwsSQLException{return0;}@OverridepublicbooleanisClosed()throwsSQLException{returnfalse;}@OverridepublicvoidsetPoolable(boolean poolable)throwsSQLException{}@OverridepublicbooleanisPoolable()throwsSQLException{returnfalse;}@OverridepublicvoidcloseOnCompletion()throwsSQLException{}@OverridepublicbooleanisCloseOnCompletion()throwsSQLException{returnfalse;}@Overridepublic<T>Tunwrap(Class<T> iface)throwsSQLException{returnnull;}@OverridepublicbooleanisWrapperFor(Class<?> iface)throwsSQLException{returnfalse;}}

JqResultSet.java

最后实现result,将数据库路径和开的表名拼接成完整csv路径,读取,并返回查询。

packagecom.dafei1288.jimsql.jdbc;importcom.google.common.io.Files;importjava.io.File;importjava.io.IOException;importjava.io.InputStream;importjava.io.Reader;importjava.math.BigDecimal;importjava.net.URI;importjava.net.URL;importjava.nio.charset.Charset;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.sql.Array;importjava.sql.Blob;importjava.sql.Clob;importjava.sql.Date;importjava.sql.NClob;importjava.sql.Ref;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.RowId;importjava.sql.SQLException;importjava.sql.SQLWarning;importjava.sql.SQLXML;importjava.sql.Statement;importjava.sql.Time;importjava.sql.Timestamp;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;publicclassJqResultSetimplementsResultSet{privateString filepath;privateString tableName;privateList<String> cols;privateList<String> lines =newArrayList<>();privateIterator<String> datas;privateString currentData;privateString[] currentArray;privateMap<String,Integer> colIndexMap =newHashMap<>();publicJqResultSet(String filepath,List<String> cols,String tableName){this.filepath = filepath;this.cols = cols;this.tableName = tableName;try{
      lines =Files.readLines(Path.of(URI.create(this.filepath+this.tableName+".csv")).toFile(),Charset.defaultCharset());String headerLine = lines.get(0);String[] headers = headerLine.split(",");for(int i =0; i < headers.length; i++){String header = headers[i];
        colIndexMap.put(header,i);}
      datas = lines.subList(1,lines.size()).iterator();}catch(IOException e){thrownewRuntimeException(e);}}@Overridepublicbooleannext()throwsSQLException{boolean hasNext = datas.hasNext();if(hasNext ==true){
      currentData = datas.next();
      currentArray = currentData.split(",");}return hasNext;}@Overridepublicvoidclose()throwsSQLException{}@OverridepublicbooleanwasNull()throwsSQLException{returnfalse;}@OverridepublicStringgetString(int columnIndex)throwsSQLException{return currentArray[columnIndex];}@OverridepublicbooleangetBoolean(int columnIndex)throwsSQLException{returnfalse;}@OverridepublicbytegetByte(int columnIndex)throwsSQLException{return0;}@OverridepublicshortgetShort(int columnIndex)throwsSQLException{return0;}@OverridepublicintgetInt(int columnIndex)throwsSQLException{return0;}@OverridepubliclonggetLong(int columnIndex)throwsSQLException{return0;}@OverridepublicfloatgetFloat(int columnIndex)throwsSQLException{return0;}@OverridepublicdoublegetDouble(int columnIndex)throwsSQLException{return0;}@OverridepublicBigDecimalgetBigDecimal(int columnIndex,int scale)throwsSQLException{returnnull;}@Overridepublicbyte[]getBytes(int columnIndex)throwsSQLException{returnnewbyte[0];}@OverridepublicDategetDate(int columnIndex)throwsSQLException{returnnull;}@OverridepublicTimegetTime(int columnIndex)throwsSQLException{returnnull;}@OverridepublicTimestampgetTimestamp(int columnIndex)throwsSQLException{returnnull;}@OverridepublicInputStreamgetAsciiStream(int columnIndex)throwsSQLException{returnnull;}@OverridepublicInputStreamgetUnicodeStream(int columnIndex)throwsSQLException{returnnull;}@OverridepublicInputStreamgetBinaryStream(int columnIndex)throwsSQLException{returnnull;}@OverridepublicStringgetString(String columnLabel)throwsSQLException{return currentArray[this.colIndexMap.get(columnLabel)];}@OverridepublicbooleangetBoolean(String columnLabel)throwsSQLException{returnfalse;}@OverridepublicbytegetByte(String columnLabel)throwsSQLException{return0;}@OverridepublicshortgetShort(String columnLabel)throwsSQLException{return0;}@OverridepublicintgetInt(String columnLabel)throwsSQLException{return0;}@OverridepubliclonggetLong(String columnLabel)throwsSQLException{return0;}@OverridepublicfloatgetFloat(String columnLabel)throwsSQLException{return0;}@OverridepublicdoublegetDouble(String columnLabel)throwsSQLException{return0;}@OverridepublicBigDecimalgetBigDecimal(String columnLabel,int scale)throwsSQLException{returnnull;}@Overridepublicbyte[]getBytes(String columnLabel)throwsSQLException{returnnewbyte[0];}@OverridepublicDategetDate(String columnLabel)throwsSQLException{returnnull;}@OverridepublicTimegetTime(String columnLabel)throwsSQLException{returnnull;}@OverridepublicTimestampgetTimestamp(String columnLabel)throwsSQLException{returnnull;}@OverridepublicInputStreamgetAsciiStream(String columnLabel)throwsSQLException{returnnull;}@OverridepublicInputStreamgetUnicodeStream(String columnLabel)throwsSQLException{returnnull;}@OverridepublicInputStreamgetBinaryStream(String columnLabel)throwsSQLException{returnnull;}@OverridepublicSQLWarninggetWarnings()throwsSQLException{returnnull;}@OverridepublicvoidclearWarnings()throwsSQLException{}@OverridepublicStringgetCursorName()throwsSQLException{returnnull;}@OverridepublicResultSetMetaDatagetMetaData()throwsSQLException{returnnull;}@OverridepublicObjectgetObject(int columnIndex)throwsSQLException{returnnull;}@OverridepublicObjectgetObject(String columnLabel)throwsSQLException{returnnull;}@OverridepublicintfindColumn(String columnLabel)throwsSQLException{return0;}@OverridepublicReadergetCharacterStream(int columnIndex)throwsSQLException{returnnull;}@OverridepublicReadergetCharacterStream(String columnLabel)throwsSQLException{returnnull;}@OverridepublicBigDecimalgetBigDecimal(int columnIndex)throwsSQLException{returnnull;}@OverridepublicBigDecimalgetBigDecimal(String columnLabel)throwsSQLException{returnnull;}@OverridepublicbooleanisBeforeFirst()throwsSQLException{returnfalse;}@OverridepublicbooleanisAfterLast()throwsSQLException{returnfalse;}@OverridepublicbooleanisFirst()throwsSQLException{returnfalse;}@OverridepublicbooleanisLast()throwsSQLException{returnfalse;}@OverridepublicvoidbeforeFirst()throwsSQLException{}@OverridepublicvoidafterLast()throwsSQLException{}@Overridepublicbooleanfirst()throwsSQLException{returnfalse;}@Overridepublicbooleanlast()throwsSQLException{returnfalse;}@OverridepublicintgetRow()throwsSQLException{return0;}@Overridepublicbooleanabsolute(int row)throwsSQLException{returnfalse;}@Overridepublicbooleanrelative(int rows)throwsSQLException{returnfalse;}@Overridepublicbooleanprevious()throwsSQLException{returnfalse;}@OverridepublicvoidsetFetchDirection(int direction)throwsSQLException{}@OverridepublicintgetFetchDirection()throwsSQLException{return0;}@OverridepublicvoidsetFetchSize(int rows)throwsSQLException{}@OverridepublicintgetFetchSize()throwsSQLException{return0;}@OverridepublicintgetType()throwsSQLException{return0;}@OverridepublicintgetConcurrency()throwsSQLException{return0;}@OverridepublicbooleanrowUpdated()throwsSQLException{returnfalse;}@OverridepublicbooleanrowInserted()throwsSQLException{returnfalse;}@OverridepublicbooleanrowDeleted()throwsSQLException{returnfalse;}@OverridepublicvoidupdateNull(int columnIndex)throwsSQLException{}@OverridepublicvoidupdateBoolean(int columnIndex,boolean x)throwsSQLException{}@OverridepublicvoidupdateByte(int columnIndex,byte x)throwsSQLException{}@OverridepublicvoidupdateShort(int columnIndex,short x)throwsSQLException{}@OverridepublicvoidupdateInt(int columnIndex,int x)throwsSQLException{}@OverridepublicvoidupdateLong(int columnIndex,long x)throwsSQLException{}@OverridepublicvoidupdateFloat(int columnIndex,float x)throwsSQLException{}@OverridepublicvoidupdateDouble(int columnIndex,double x)throwsSQLException{}@OverridepublicvoidupdateBigDecimal(int columnIndex,BigDecimal x)throwsSQLException{}@OverridepublicvoidupdateString(int columnIndex,String x)throwsSQLException{}@OverridepublicvoidupdateBytes(int columnIndex,byte[] x)throwsSQLException{}@OverridepublicvoidupdateDate(int columnIndex,Date x)throwsSQLException{}@OverridepublicvoidupdateTime(int columnIndex,Time x)throwsSQLException{}@OverridepublicvoidupdateTimestamp(int columnIndex,Timestamp x)throwsSQLException{}@OverridepublicvoidupdateAsciiStream(int columnIndex,InputStream x,int length)throwsSQLException{}@OverridepublicvoidupdateBinaryStream(int columnIndex,InputStream x,int length)throwsSQLException{}@OverridepublicvoidupdateCharacterStream(int columnIndex,Reader x,int length)throwsSQLException{}@OverridepublicvoidupdateObject(int columnIndex,Object x,int scaleOrLength)throwsSQLException{}@OverridepublicvoidupdateObject(int columnIndex,Object x)throwsSQLException{}@OverridepublicvoidupdateNull(String columnLabel)throwsSQLException{}@OverridepublicvoidupdateBoolean(String columnLabel,boolean x)throwsSQLException{}@OverridepublicvoidupdateByte(String columnLabel,byte x)throwsSQLException{}@OverridepublicvoidupdateShort(String columnLabel,short x)throwsSQLException{}@OverridepublicvoidupdateInt(String columnLabel,int x)throwsSQLException{}@OverridepublicvoidupdateLong(String columnLabel,long x)throwsSQLException{}@OverridepublicvoidupdateFloat(String columnLabel,float x)throwsSQLException{}@OverridepublicvoidupdateDouble(String columnLabel,double x)throwsSQLException{}@OverridepublicvoidupdateBigDecimal(String columnLabel,BigDecimal x)throwsSQLException{}@OverridepublicvoidupdateString(String columnLabel,String x)throwsSQLException{}@OverridepublicvoidupdateBytes(String columnLabel,byte[] x)throwsSQLException{}@OverridepublicvoidupdateDate(String columnLabel,Date x)throwsSQLException{}@OverridepublicvoidupdateTime(String columnLabel,Time x)throwsSQLException{}@OverridepublicvoidupdateTimestamp(String columnLabel,Timestamp x)throwsSQLException{}@OverridepublicvoidupdateAsciiStream(String columnLabel,InputStream x,int length)throwsSQLException{}@OverridepublicvoidupdateBinaryStream(String columnLabel,InputStream x,int length)throwsSQLException{}@OverridepublicvoidupdateCharacterStream(String columnLabel,Reader reader,int length)throwsSQLException{}@OverridepublicvoidupdateObject(String columnLabel,Object x,int scaleOrLength)throwsSQLException{}@OverridepublicvoidupdateObject(String columnLabel,Object x)throwsSQLException{}@OverridepublicvoidinsertRow()throwsSQLException{}@OverridepublicvoidupdateRow()throwsSQLException{}@OverridepublicvoiddeleteRow()throwsSQLException{}@OverridepublicvoidrefreshRow()throwsSQLException{}@OverridepublicvoidcancelRowUpdates()throwsSQLException{}@OverridepublicvoidmoveToInsertRow()throwsSQLException{}@OverridepublicvoidmoveToCurrentRow()throwsSQLException{}@OverridepublicStatementgetStatement()throwsSQLException{returnnull;}@OverridepublicObjectgetObject(int columnIndex,Map<String,Class<?>> map)throwsSQLException{returnnull;}@OverridepublicRefgetRef(int columnIndex)throwsSQLException{returnnull;}@OverridepublicBlobgetBlob(int columnIndex)throwsSQLException{returnnull;}@OverridepublicClobgetClob(int columnIndex)throwsSQLException{returnnull;}@OverridepublicArraygetArray(int columnIndex)throwsSQLException{returnnull;}@OverridepublicObjectgetObject(String columnLabel,Map<String,Class<?>> map)throwsSQLException{returnnull;}@OverridepublicRefgetRef(String columnLabel)throwsSQLException{returnnull;}@OverridepublicBlobgetBlob(String columnLabel)throwsSQLException{returnnull;}@OverridepublicClobgetClob(String columnLabel)throwsSQLException{returnnull;}@OverridepublicArraygetArray(String columnLabel)throwsSQLException{returnnull;}@OverridepublicDategetDate(int columnIndex,Calendar cal)throwsSQLException{returnnull;}@OverridepublicDategetDate(String columnLabel,Calendar cal)throwsSQLException{returnnull;}@OverridepublicTimegetTime(int columnIndex,Calendar cal)throwsSQLException{returnnull;}@OverridepublicTimegetTime(String columnLabel,Calendar cal)throwsSQLException{returnnull;}@OverridepublicTimestampgetTimestamp(int columnIndex,Calendar cal)throwsSQLException{returnnull;}@OverridepublicTimestampgetTimestamp(String columnLabel,Calendar cal)throwsSQLException{returnnull;}@OverridepublicURLgetURL(int columnIndex)throwsSQLException{returnnull;}@OverridepublicURLgetURL(String columnLabel)throwsSQLException{returnnull;}@OverridepublicvoidupdateRef(int columnIndex,Ref x)throwsSQLException{}@OverridepublicvoidupdateRef(String columnLabel,Ref x)throwsSQLException{}@OverridepublicvoidupdateBlob(int columnIndex,Blob x)throwsSQLException{}@OverridepublicvoidupdateBlob(String columnLabel,Blob x)throwsSQLException{}@OverridepublicvoidupdateClob(int columnIndex,Clob x)throwsSQLException{}@OverridepublicvoidupdateClob(String columnLabel,Clob x)throwsSQLException{}@OverridepublicvoidupdateArray(int columnIndex,Array x)throwsSQLException{}@OverridepublicvoidupdateArray(String columnLabel,Array x)throwsSQLException{}@OverridepublicRowIdgetRowId(int columnIndex)throwsSQLException{returnnull;}@OverridepublicRowIdgetRowId(String columnLabel)throwsSQLException{returnnull;}@OverridepublicvoidupdateRowId(int columnIndex,RowId x)throwsSQLException{}@OverridepublicvoidupdateRowId(String columnLabel,RowId x)throwsSQLException{}@OverridepublicintgetHoldability()throwsSQLException{return0;}@OverridepublicbooleanisClosed()throwsSQLException{returnfalse;}@OverridepublicvoidupdateNString(int columnIndex,String nString)throwsSQLException{}@OverridepublicvoidupdateNString(String columnLabel,String nString)throwsSQLException{}@OverridepublicvoidupdateNClob(int columnIndex,NClob nClob)throwsSQLException{}@OverridepublicvoidupdateNClob(String columnLabel,NClob nClob)throwsSQLException{}@OverridepublicNClobgetNClob(int columnIndex)throwsSQLException{returnnull;}@OverridepublicNClobgetNClob(String columnLabel)throwsSQLException{returnnull;}@OverridepublicSQLXMLgetSQLXML(int columnIndex)throwsSQLException{returnnull;}@OverridepublicSQLXMLgetSQLXML(String columnLabel)throwsSQLException{returnnull;}@OverridepublicvoidupdateSQLXML(int columnIndex,SQLXML xmlObject)throwsSQLException{}@OverridepublicvoidupdateSQLXML(String columnLabel,SQLXML xmlObject)throwsSQLException{}@OverridepublicStringgetNString(int columnIndex)throwsSQLException{returnnull;}@OverridepublicStringgetNString(String columnLabel)throwsSQLException{returnnull;}@OverridepublicReadergetNCharacterStream(int columnIndex)throwsSQLException{returnnull;}@OverridepublicReadergetNCharacterStream(String columnLabel)throwsSQLException{returnnull;}@OverridepublicvoidupdateNCharacterStream(int columnIndex,Reader x,long length)throwsSQLException{}@OverridepublicvoidupdateNCharacterStream(String columnLabel,Reader reader,long length)throwsSQLException{}@OverridepublicvoidupdateAsciiStream(int columnIndex,InputStream x,long length)throwsSQLException{}@OverridepublicvoidupdateBinaryStream(int columnIndex,InputStream x,long length)throwsSQLException{}@OverridepublicvoidupdateCharacterStream(int columnIndex,Reader x,long length)throwsSQLException{}@OverridepublicvoidupdateAsciiStream(String columnLabel,InputStream x,long length)throwsSQLException{}@OverridepublicvoidupdateBinaryStream(String columnLabel,InputStream x,long length)throwsSQLException{}@OverridepublicvoidupdateCharacterStream(String columnLabel,Reader reader,long length)throwsSQLException{}@OverridepublicvoidupdateBlob(int columnIndex,InputStream inputStream,long length)throwsSQLException{}@OverridepublicvoidupdateBlob(String columnLabel,InputStream inputStream,long length)throwsSQLException{}@OverridepublicvoidupdateClob(int columnIndex,Reader reader,long length)throwsSQLException{}@OverridepublicvoidupdateClob(String columnLabel,Reader reader,long length)throwsSQLException{}@OverridepublicvoidupdateNClob(int columnIndex,Reader reader,long length)throwsSQLException{}@OverridepublicvoidupdateNClob(String columnLabel,Reader reader,long length)throwsSQLException{}@OverridepublicvoidupdateNCharacterStream(int columnIndex,Reader x)throwsSQLException{}@OverridepublicvoidupdateNCharacterStream(String columnLabel,Reader reader)throwsSQLException{}@OverridepublicvoidupdateAsciiStream(int columnIndex,InputStream x)throwsSQLException{}@OverridepublicvoidupdateBinaryStream(int columnIndex,InputStream x)throwsSQLException{}@OverridepublicvoidupdateCharacterStream(int columnIndex,Reader x)throwsSQLException{}@OverridepublicvoidupdateAsciiStream(String columnLabel,InputStream x)throwsSQLException{}@OverridepublicvoidupdateBinaryStream(String columnLabel,InputStream x)throwsSQLException{}@OverridepublicvoidupdateCharacterStream(String columnLabel,Reader reader)throwsSQLException{}@OverridepublicvoidupdateBlob(int columnIndex,InputStream inputStream)throwsSQLException{}@OverridepublicvoidupdateBlob(String columnLabel,InputStream inputStream)throwsSQLException{}@OverridepublicvoidupdateClob(int columnIndex,Reader reader)throwsSQLException{}@OverridepublicvoidupdateClob(String columnLabel,Reader reader)throwsSQLException{}@OverridepublicvoidupdateNClob(int columnIndex,Reader reader)throwsSQLException{}@OverridepublicvoidupdateNClob(String columnLabel,Reader reader)throwsSQLException{}@Overridepublic<T>TgetObject(int columnIndex,Class<T> type)throwsSQLException{returnnull;}@Overridepublic<T>TgetObject(String columnLabel,Class<T> type)throwsSQLException{returnnull;}@Overridepublic<T>Tunwrap(Class<T> iface)throwsSQLException{returnnull;}@OverridepublicbooleanisWrapperFor(Class<?> iface)throwsSQLException{returnfalse;}}

测试

最后我们进行测试

user.csv

如下

id,name,age
1,jacky,22
2,doudou,3
3,maimai,3

测试类如下

importjava.io.File;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassTest{publicstaticvoidmain(String[] args)throwsException{Class.forName("com.dafei1288.jimsql.jdbc.JqDriver");Connection conn =DriverManager.getConnection("jdbc:jimsql:file:///D:/working/opensource/jimsql/jdbc/src/test/resources/");System.out.println(conn);Statement statement = conn.createStatement();System.out.println(statement);ResultSet resultSet = statement.executeQuery("select * from user");System.out.println(resultSet);while(resultSet.next()){//      id,name,ageString id = resultSet.getString("id");String name = resultSet.getString("name");String age = resultSet.getString("age");System.out.println(String.format("row ==> id : %s , name : %s , age = %s",id,name,age));}}}

在这里插入图片描述


本文转载自: https://blog.csdn.net/dafei1288/article/details/125222681
版权归原作者 麒思妙想 所有, 如有侵权,请联系我们删除。

“如何只用4步,实现一个自定义JDBC驱动?”的评论:

还没有评论