前段时间写过一篇:
具体Connection类实现:
好了,使用我自己这套数据库驱动连接wjw数据库,执行sql语句吧:
运行结果:
JAVA JDBC使用相同代码连接不同数据库原理浅解析 ,但是再明白也不如自己实现个数据库驱动理解的明白.
今日,本人化身数据库服务提供商,推出了wjw数据库,
,获得了it界一篇好评,但是我得给JAVA开发一套驱动包,与JAVA接轨,让JAVA程序员也能使用我这款很厉害的wjw数据库啊.
![敲打](http://static.blog.csdn.net/xheditor/xheditor_emot/default/knock.gif)
示例代码如下(主要是实现JDBC接口):
数据库驱动类:
public class Driver implements java.sql.Driver{
static{
//运行Class.forName("特定数据库驱动类");将本类加载到内存中(本类属于wjw数据厂商自己实现,用来给用户提供链接)
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean acceptsURL(String url) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public Connection connect(String url, Properties info) throws SQLException {
// TODO Auto-generated method stub
System.out.println("返回wjw数据库的连接!");
return new WjwConnection();
}
public int getMajorVersion() {
// TODO Auto-generated method stub
return 0;
}
public int getMinorVersion() {
// TODO Auto-generated method stub
return 0;
}
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean jdbcCompliant() {
// TODO Auto-generated method stub
return false;
}
}
具体Connection类实现:
public class WjwConnection implements java.sql.Connection{
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub
}
public void close() throws SQLException {
// TODO Auto-generated method stub
}
public void commit() throws SQLException {
// TODO Auto-generated method stub
}
public Array createArrayOf(String typeName, Object[] elements)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Blob createBlob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Clob createClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public NClob createNClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public SQLXML createSQLXML() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Statement createStatement() throws SQLException {
// TODO Auto-generated method stub
System.out.println("执行wjw数据库具体Connection的createStatement方法!");
return new WjwStatement();
}
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Statement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Properties getClientInfo() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public String getClientInfo(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean isValid(int timeout) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public String nativeSQL(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, String[] columnNames)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
}
public void rollback() throws SQLException {
// TODO Auto-generated method stub
}
public void rollback(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
// TODO Auto-generated method stub
}
public void setCatalog(String catalog) throws SQLException {
// TODO Auto-generated method stub
}
public void setClientInfo(Properties properties)
throws SQLClientInfoException {
// TODO Auto-generated method stub
}
public void setClientInfo(String name, String value)
throws SQLClientInfoException {
// TODO Auto-generated method stub
}
public void setHoldability(int holdability) throws SQLException {
// TODO Auto-generated method stub
}
public void setReadOnly(boolean readOnly) throws SQLException {
// TODO Auto-generated method stub
}
public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Savepoint setSavepoint(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void setTransactionIsolation(int level) throws SQLException {
// TODO Auto-generated method stub
}
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
// TODO Auto-generated method stub
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
具体Statement类实现:
public class WjwStatement implements java.sql.Statement{
public void addBatch(String sql) throws SQLException {
// TODO Auto-generated method stub
}
public void cancel() throws SQLException {
// TODO Auto-generated method stub
}
public void clearBatch() throws SQLException {
// TODO Auto-generated method stub
}
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub
}
public void close() throws SQLException {
// TODO Auto-generated method stub
}
public boolean execute(String sql) throws SQLException {
// TODO Auto-generated method stub
System.out.println("在wjw数据库中执行sql语句!");
return true;
}
public boolean execute(String sql, int autoGeneratedKeys)
throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean execute(String sql, String[] columnNames)
throws SQLException {
// TODO Auto-generated method stub
return false;
}
public int[] executeBatch() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public ResultSet executeQuery(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int executeUpdate(String sql) throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public int executeUpdate(String sql, int autoGeneratedKeys)
throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public int executeUpdate(String sql, int[] columnIndexes)
throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public int executeUpdate(String sql, String[] columnNames)
throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public Connection getConnection() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getFetchDirection() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public int getFetchSize() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public ResultSet getGeneratedKeys() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getMaxFieldSize() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public int getMaxRows() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public boolean getMoreResults() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean getMoreResults(int current) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public int getQueryTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public ResultSet getResultSet() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getResultSetConcurrency() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public int getResultSetHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public int getResultSetType() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public int getUpdateCount() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean isPoolable() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public void setCursorName(String name) throws SQLException {
// TODO Auto-generated method stub
}
public void setEscapeProcessing(boolean enable) throws SQLException {
// TODO Auto-generated method stub
}
public void setFetchDirection(int direction) throws SQLException {
// TODO Auto-generated method stub
}
public void setFetchSize(int rows) throws SQLException {
// TODO Auto-generated method stub
}
public void setMaxFieldSize(int max) throws SQLException {
// TODO Auto-generated method stub
}
public void setMaxRows(int max) throws SQLException {
// TODO Auto-generated method stub
}
public void setPoolable(boolean poolable) throws SQLException {
// TODO Auto-generated method stub
}
public void setQueryTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
好了,使用我自己这套数据库驱动连接wjw数据库,执行sql语句吧:
Main类如下:
public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.wjw.jdbc.Driver");
Connection connection = DriverManager.getConnection("wjw数据库连接字符串");
Statement statement = connection.createStatement();
boolean isexecute = statement.execute("wjw数据库sql语句!");
if(isexecute){
System.out.println("数据库sql执行成功!");
}else{
System.out.println("数据库sql执行失败!");
}
}
}
运行结果:
返回wjw数据库的连接!
执行wjw数据库具体Connection的createStatement方法!
在wjw数据库中执行sql语句!
数据库sql执行成功!
执行wjw数据库具体Connection的createStatement方法!
在wjw数据库中执行sql语句!
数据库sql执行成功!
Main类最重要的就是这句:
Class.forName("com.wjw.jdbc.Driver");
他将我自己的驱动类字节码加载到内存中,同时执行静态代码块,将wjw的驱动对象注册到DeiverManager中,从此我们java程序和wjw数据库才真正的联系起来,后面一片坦途,金光大道.
玩笑的小程序实例
希望对大家理解JDBC这种机制有些许帮助吧(结合另一篇博文理解效果更好)
![疑问](http://static.blog.csdn.net/xheditor/xheditor_emot/default/doubt.gif)
end