JDBC接口分析

DriverManager

在DriverManager加载时,会在静态代码块中调用loadInitialDrivers()加载默认jdbc驱动,默认会加载在系统属性jdbc.drivers中配置的驱动及通过SPI方式加载Driver.class,各驱动的Driver实现类会调用DriverManager.registerDriver()注册驱动。目前主流的驱动基本都使用SPI的方式加载,所以大部分驱动只需要引入依赖包既可,不需要手动注册。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class DriverManager {
// 保存已经加载的jdbc Driver
private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<>();
static {
loadInitialDrivers();
}
// 加载默认jdbc驱动
private static void loadInitialDrivers() {
String drivers;
//获取系统属性jdbc.drivers,并加载已配置驱动
try {
drivers = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty("jdbc.drivers");
}
});
} catch (Exception ex) {
drivers = null;
}
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
//spi的方式加载Driver驱动
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
Iterator<Driver> driversIterator = loadedDrivers.iterator();
try{
while(driversIterator.hasNext()) {
driversIterator.next();
}
} catch(Throwable t) {
}
return null;
}
});
if (drivers == null || drivers.equals("")) {
return;
}
String[] driversList = drivers.split(":");
for (String aDriver : driversList) {
try {
Class.forName(aDriver, true, ClassLoader.getSystemClassLoader());
} catch (Exception ex) {
println("DriverManager.Initialize: load failed: " + ex);
}
}
}
}

驱动管理相关方法,包括注册驱动、取消注册驱动,获取所有驱动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class DriverManager {
// 保存已经加载的jdbc Driver
private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<>();
// 注册jdbc驱动
public static synchronized void registerDriver(java.sql.Driver driver) throws SQLException {
registerDriver(driver, null);
}
// 注册jdbc驱动
public static synchronized void registerDriver(java.sql.Driver driver,DriverAction da)
throws SQLException {
if(driver != null) {
registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
} else {
throw new NullPointerException();
}
println("registerDriver: " + driver);

}
//取消注册驱动
@CallerSensitive
public static synchronized void deregisterDriver(Driver driver)
throws SQLException {
if (driver == null) {
return;
}
SecurityManager sec = System.getSecurityManager();
if (sec != null) {
sec.checkPermission(DEREGISTER_DRIVER_PERMISSION);
}
DriverInfo aDriver = new DriverInfo(driver, null);
if(registeredDrivers.contains(aDriver)) {
if (isDriverAllowed(driver, Reflection.getCallerClass())) {
DriverInfo di = registeredDrivers.get(registeredDrivers.indexOf(aDriver));
if(di.action() != null) {
di.action().deregister();
}
registeredDrivers.remove(aDriver);
} else {
throw new SecurityException();
}
} else {
}
}
//获取所有的jdbc驱动
@CallerSensitive
public static java.util.Enumeration<Driver> getDrivers() {
java.util.Vector<Driver> result = new java.util.Vector<>();
Class<?> callerClass = Reflection.getCallerClass();
for(DriverInfo aDriver : registeredDrivers) {
if(isDriverAllowed(aDriver.driver, callerClass)) {
result.addElement(aDriver.driver);
} else {
println(" skipping: " + aDriver.getClass().getName());
}
}
return (result.elements());
}
}

获取连接相关方法getConnection有多个重载方法,其最后都调用getConnection(String url, java.util.Properties info, Class<?> caller)方法。
最终会调用驱动的connect()方法获取连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class DriverManager {
// 获取jdbc连接
@CallerSensitive
public static Connection getConnection(String url,java.util.Properties info) throws SQLException {
return (getConnection(url, info, Reflection.getCallerClass()));
}
// 获取jdbc连接
public static Connection getConnection(String urlString user, String password) throws SQLException {
java.util.Properties info = new java.util.Properties();
if (user != null) {
info.put("user", user);
}
if (password != null) {
info.put("password", password);
}
return (getConnection(url, info, Reflection.getCallerClass()));
}
// 获取jdbc连接
public static Connection getConnection(String url) throws SQLException {
java.util.Properties info = new java.util.Properties();
return (getConnection(url, info, Reflection.getCallerClass()));
}
// 获取Driver
@CallerSensitive
public static Driver getDriver(String url)throws SQLException {
Class<?> callerClass = Reflection.getCallerClass();
for (DriverInfo aDriver : registeredDrivers) {
if(isDriverAllowed(aDriver.driver, callerClass)) {
try {
if(aDriver.driver.acceptsURL(url)) {
return (aDriver.driver);
}
} catch(SQLException sqe) {
}
} else {
}
}
throw new SQLException("No suitable driver", "08001");
}
private static Connection getConnection(String url, java.util.Properties info, Class<?> caller) throws SQLException {
//获取调用类的类加载器,也就是调用DriverManager.getConnection()的类
ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
synchronized(DriverManager.class) {
if (callerCL == null) {
callerCL = Thread.currentThread().getContextClassLoader();
}
}
if(url == null) {
throw new SQLException("The url cannot be null", "08001");
}
SQLException reason = null;
for(DriverInfo aDriver : registeredDrivers) {
//确保记载Driver的类加载器调用类的类加载器是同一个
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
return (con);
}
} catch (SQLException ex) {
if (reason == null) {
reason = ex;
}
}
} else {
}
}
if (reason != null) {
throw reason;
}
throw new SQLException("No suitable driver found for "+ url, "08001");
}


}

总结:DriverManager有两个作用,一是用于管理jdbc驱动,二是用于获取jdbc连接,

Statement

  • 普通sql查询
  • 支持批量更新,批量删除;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
public interface Statement extends Wrapper, AutoCloseable {
//执行查询,返回结果集 SELECT
ResultSet executeQuery(String sql) throws SQLException;
//执行更新sql语句, INSERT、UPDATE、DELETE,返回影响行数
int executeUpdate(String sql) throws SQLException;
void close() throws SQLException;
//获取statement对象所生成resultSet 象中的字符和二进制列值返回的最大字节数。此限制仅应用于 binary、varbinary、longvarbinary、char、varchar、nchar、nvarchar、longnvarchar和longvarchar 列。如果超过了该限制,则直接丢弃多出的数据。
int getMaxFieldSize() throws SQLException;
void setMaxFieldSize(int max) throws SQLException;
//获取 ResultSet能返回的最大行数
int getMaxRows() throws SQLException;
void setMaxRows(int max) throws SQLException;
//enable为true表示启用转义处理;为 false 表示禁用转义处理
void setEscapeProcessing(boolean enable) throws SQLException;
//查询超时时间
int getQueryTimeout() throws SQLException;
void setQueryTimeout(int seconds) throws SQLException;
//取消查询
void cancel() throws SQLException;
//获取此 Statement 对象上的调用报告的第一个警告
SQLWarning getWarnings() throws SQLException;
//清除在此 Statement 对象上报告的所有警告
void clearWarnings() throws SQLException;
//光标名称设置为给定的 String,后续 Statement 对象的 execute 方法将使用此字符串
void setCursorName(String name) throws SQLException;
//----------------------- Multiple Results --------------------------
//执行sql语句,如果第一个结果为 ResultSet 对象,则返回 true;如果其为更新计数或者不存在任何结果,则返回 false
boolean execute(String sql) throws SQLException;
//获取结果集,配合execute()使用
ResultSet getResultSet() throws SQLException;
//获取影响行数,配合execute()使用
int getUpdateCount() throws SQLException;
//移动后续结果,如果其为 ResultSet 对象,则返回 true,配合execute()使用
boolean getMoreResults() throws SQLException;
//--------------------------JDBC 2.0-----------------------------
//设置结果集游标的方向,默认值是 ResultSet.FETCH_FORWARD ResultSet.FETCH_FORWARD,ResultSet.FETCH_REVERSE,ResultSet.FETCH_UNKNOWN
void setFetchDirection(int direction) throws SQLException;
int getFetchDirection() throws SQLException;
//控制的是从数据库向应用程序客户端发送数据的页面大小
void setFetchSize(int rows) throws SQLException;
int getFetchSize() throws SQLException;
int getResultSetConcurrency() throws SQLException;
//设置 ResultSet type ResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.TYPE_SCROLL_SENSITIVE
int getResultSetType() throws SQLException;
void addBatch( String sql ) throws SQLException;
void clearBatch() throws SQLException;
int[] executeBatch() throws SQLException;
Connection getConnection() throws SQLException;
//--------------------------JDBC 3.0-----------------------------
int CLOSE_CURRENT_RESULT = 1;
int KEEP_CURRENT_RESULT = 2;
int CLOSE_ALL_RESULTS = 3;
int SUCCESS_NO_INFO = -2;
int EXECUTE_FAILED = -3;
int RETURN_GENERATED_KEYS = 1;
int NO_GENERATED_KEYS = 2;
boolean getMoreResults(int current) throws SQLException;
ResultSet getGeneratedKeys() throws SQLException;
int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException;
int executeUpdate(String sql, int columnIndexes[]) throws SQLException;
int executeUpdate(String sql, String columnNames[]) throws SQLException;
boolean execute(String sql, int autoGeneratedKeys) throws SQLException;
boolean execute(String sql, int columnIndexes[]) throws SQLException;
boolean execute(String sql, String columnNames[]) throws SQLException;
int getResultSetHoldability() throws SQLException;
boolean isClosed() throws SQLException;
void setPoolable(boolean poolable)throws SQLException;
boolean isPoolable()throws SQLException;
//--------------------------JDBC 4.1 -----------------------------
//语句所有依赖的结果集都被关闭时
public void closeOnCompletion() throws SQLException;
public boolean isCloseOnCompletion() throws SQLException;
//--------------------------JDBC 4.2 -----------------------------
//增加了对大数据的处理(一般不用)
default long getLargeUpdateCount() throws SQLException {
throw new UnsupportedOperationException("getLargeUpdateCount not implemented");
}
default void setLargeMaxRows(long max) throws SQLException {
throw new UnsupportedOperationException("setLargeMaxRows not implemented");
}
default long getLargeMaxRows() throws SQLException {
return 0;
}
default long[] executeLargeBatch() throws SQLException {
throw new UnsupportedOperationException("executeLargeBatch not implemented");
}
default long executeLargeUpdate(String sql) throws SQLException {
throw new UnsupportedOperationException("executeLargeUpdate not implemented");
}
default long executeLargeUpdate(String sql, int autoGeneratedKeys)
throws SQLException {
throw new SQLFeatureNotSupportedException("executeLargeUpdate not implemented");
}
default long executeLargeUpdate(String sql, int columnIndexes[]) throws SQLException {
throw new SQLFeatureNotSupportedException("executeLargeUpdate not implemented");
}
default long executeLargeUpdate(String sql, String columnNames[])
throws SQLException {
throw new SQLFeatureNotSupportedException("executeLargeUpdate not implemented");
}
}

PreparedStatement

  • PreparedStatement会预编译,会被缓冲,在缓存区中可以发现预编译的命令,虽然会被再次解析,但不会被再次编译,能够有效提高系统性能
  • PreparedStatement能够预防SQL注入攻击
  • PreparedStatement继承了Statement,添加了处理输入参数的方法setXX
  • PreparedStatement定义了execute、executeQuery、addBatch
  • 可变参数的SQL,编译一次,执行多次,效率高;
  • 安全性好,有效防止Sql注入等问题;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public interface PreparedStatement extends Statement {
ResultSet executeQuery() throws SQLException;
int executeUpdate() throws SQLException;
void setNull(int parameterIndex, int sqlType) throws SQLException;
void setBoolean(int parameterIndex, boolean x) throws SQLException;
void setByte(int parameterIndex, byte x) throws SQLException;
void setShort(int parameterIndex, short x) throws SQLException;
void setInt(int parameterIndex, int x) throws SQLException;
void setLong(int parameterIndex, long x) throws SQLException;
void setFloat(int parameterIndex, float x) throws SQLException;
void setDouble(int parameterIndex, double x) throws SQLException;
void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException;
void setString(int parameterIndex, String x) throws SQLException;
void setBytes(int parameterIndex, byte x[]) throws SQLException;
void setDate(int parameterIndex, java.sql.Date x)throws SQLException;
void setTime(int parameterIndex, java.sql.Time x)throws SQLException;
void setTimestamp(int parameterIndex, java.sql.Timestamp x)throws SQLException;
void setAsciiStream(int parameterIndex, java.io.InputStream x, int length)throws SQLException;
void setUnicodeStream(int parameterIndex, java.io.InputStream x,int length) throws SQLException;
void setBinaryStream(int parameterIndex, java.io.InputStream x,int length) throws SQLException;
//----------------------------------------------------------------------
void setObject(int parameterIndex, Object x, int targetSqlType)throws SQLException;
void setObject(int parameterIndex, Object x) throws SQLException;
boolean execute() throws SQLException;
//--------------------------JDBC 2.0-----------------------------
void addBatch() throws SQLException;
void setCharacterStream(int parameterIndex,java.io.Reader reader,int length) throws SQLException;
void setRef (int parameterIndex, Ref x) throws SQLException;
void setBlob (int parameterIndex, Blob x) throws SQLException;
void setClob (int parameterIndex, Clob x) throws SQLException;
void setArray (int parameterIndex, Array x) throws SQLException;
ResultSetMetaData getMetaData() throws SQLException;
void setDate(int parameterIndex, java.sql.Date x, Calendar cal)throws SQLException;
void setTime(int parameterIndex, java.sql.Time x, Calendar cal)throws SQLException;
void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal)throws SQLException;
void setNull (int parameterIndex, int sqlType, String typeName)throws SQLException;
//------------------------- JDBC 3.0 -----------------------------------
void setURL(int parameterIndex, java.net.URL x) throws SQLException;
ParameterMetaData getParameterMetaData() throws SQLException;
//------------------------- JDBC 4.0 -----------------------------------
void setRowId(int parameterIndex, RowId x) throws SQLException;
void setNString(int parameterIndex, String value) throws SQLException;
void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException;
void setNClob(int parameterIndex, NClob value) throws SQLException;
void setClob(int parameterIndex, Reader reader, long length)throws SQLException;
void setBlob(int parameterIndex, InputStream inputStream, long length)throws SQLException;
void setNClob(int parameterIndex, Reader reader, long length)throws SQLException;
void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException;
void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength)throws SQLException;
void setAsciiStream(int parameterIndex, java.io.InputStream x, long length)throws SQLException;
void setBinaryStream(int parameterIndex, java.io.InputStream x,long length) throws SQLException;
void setCharacterStream(int parameterIndex,java.io.Reader reader,long length) throws SQLException;
void setAsciiStream(int parameterIndex, java.io.InputStream x)throws SQLException;
void setBinaryStream(int parameterIndex, java.io.InputStream x)throws SQLException;
void setCharacterStream(int parameterIndex,java.io.Reader reader) throws SQLException;
void setNCharacterStream(int parameterIndex, Reader value) throws SQLException;
void setClob(int parameterIndex, Reader reader)throws SQLException;
void setBlob(int parameterIndex, InputStream inputStream)throws SQLException;
void setNClob(int parameterIndex, Reader reader)throws SQLException;
//------------------------- JDBC 4.2 -----------------------------------
default void setObject(int parameterIndex, Object x, SQLType targetSqlType,int scaleOrLength) throws SQLException {
throw new SQLFeatureNotSupportedException("setObject not implemented");
}
default void setObject(int parameterIndex, Object x, SQLType targetSqlType)throws SQLException {
throw new SQLFeatureNotSupportedException("setObject not implemented");
}
default long executeLargeUpdate() throws SQLException {
throw new UnsupportedOperationException("executeLargeUpdate not implemented");
}
}

CallableStatement

  • CallableStatement继承自prepareStatement,实现了存储过程函数调用的方法以及对于输出的处理
  • 扩展了getXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
public interface CallableStatement extends PreparedStatement {
void registerOutParameter(int parameterIndex, int sqlType)throws SQLException;
void registerOutParameter(int parameterIndex, int sqlType, int scale)throws SQLException;
boolean wasNull() throws SQLException;
String getString(int parameterIndex) throws SQLException;
boolean getBoolean(int parameterIndex) throws SQLException;
byte getByte(int parameterIndex) throws SQLException;
short getShort(int parameterIndex) throws SQLException;
int getInt(int parameterIndex) throws SQLException;
long getLong(int parameterIndex) throws SQLException;
float getFloat(int parameterIndex) throws SQLException;
double getDouble(int parameterIndex) throws SQLException;
@Deprecated
BigDecimal getBigDecimal(int parameterIndex, int scale)throws SQLException;
byte[] getBytes(int parameterIndex) throws SQLException;
java.sql.Date getDate(int parameterIndex) throws SQLException;
java.sql.Time getTime(int parameterIndex) throws SQLException;
java.sql.Timestamp getTimestamp(int parameterIndex)throws SQLException;
//----------------------------------------------------------------------
Object getObject(int parameterIndex) throws SQLException;
//--------------------------JDBC 2.0-----------------------------
BigDecimal getBigDecimal(int parameterIndex) throws SQLException;
Object getObject(int parameterIndex, java.util.Map<String,Class<?>> map)throws SQLException;
Ref getRef (int parameterIndex) throws SQLException;
Blob getBlob (int parameterIndex) throws SQLException;
Clob getClob (int parameterIndex) throws SQLException;
Array getArray (int parameterIndex) throws SQLException;
java.sql.Date getDate(int parameterIndex, Calendar cal)throws SQLException;
java.sql.Time getTime(int parameterIndex, Calendar cal)throws SQLException;
java.sql.Timestamp getTimestamp(int parameterIndex, Calendar cal)throws SQLException;
void registerOutParameter (int parameterIndex, int sqlType, String typeName)throws SQLException;
//--------------------------JDBC 3.0-----------------------------
void registerOutParameter(String parameterName, int sqlType)throws SQLException;
void registerOutParameter(String parameterName, int sqlType, int scale)throws SQLException;
void registerOutParameter (String parameterName, int sqlType, String typeName)throws SQLException;
java.net.URL getURL(int parameterIndex) throws SQLException;
void setURL(String parameterName, java.net.URL val) throws SQLException;
void setNull(String parameterName, int sqlType) throws SQLException;
void setBoolean(String parameterName, boolean x) throws SQLException;
void setByte(String parameterName, byte x) throws SQLException;
void setShort(String parameterName, short x) throws SQLException;
void setInt(String parameterName, int x) throws SQLException;
void setLong(String parameterName, long x) throws SQLException;
void setFloat(String parameterName, float x) throws SQLException;
void setDouble(String parameterName, double x) throws SQLException;
void setBigDecimal(String parameterName, BigDecimal x) throws SQLException;
void setString(String parameterName, String x) throws SQLException;
void setBytes(String parameterName, byte x[]) throws SQLException;
void setDate(String parameterName, java.sql.Date x)throws SQLException;
void setTime(String parameterName, java.sql.Time x)throws SQLException;
void setTimestamp(String parameterName, java.sql.Timestamp x)throws SQLException;
void setAsciiStream(String parameterName, java.io.InputStream x, int length)throws SQLException;
void setBinaryStream(String parameterName, java.io.InputStream x,int length) throws SQLException;
void setObject(String parameterName, Object x, int targetSqlType, int scale)throws SQLException;
void setObject(String parameterName, Object x, int targetSqlType)throws SQLException;
void setObject(String parameterName, Object x) throws SQLException;
void setCharacterStream(String parameterName,java.io.Reader reader,int length) throws SQLException;
void setDate(String parameterName, java.sql.Date x, Calendar cal)throws SQLException;
void setTime(String parameterName, java.sql.Time x, Calendar cal)throws SQLException;
void setTimestamp(String parameterName, java.sql.Timestamp x, Calendar cal)throws SQLException;
void setNull (String parameterName, int sqlType, String typeName)throws SQLException;
String getString(String parameterName) throws SQLException;
boolean getBoolean(String parameterName) throws SQLException;
byte getByte(String parameterName) throws SQLException;
short getShort(String parameterName) throws SQLException;
int getInt(String parameterName) throws SQLException;
long getLong(String parameterName) throws SQLException;
float getFloat(String parameterName) throws SQLException;
double getDouble(String parameterName) throws SQLException;
byte[] getBytes(String parameterName) throws SQLException;
java.sql.Date getDate(String parameterName) throws SQLException;
java.sql.Time getTime(String parameterName) throws SQLException;
java.sql.Timestamp getTimestamp(String parameterName) throws SQLException;
Object getObject(String parameterName) throws SQLException;
BigDecimal getBigDecimal(String parameterName) throws SQLException;
Object getObject(String parameterName, java.util.Map<String,Class<?>> map)throws SQLException;
Ref getRef (String parameterName) throws SQLException;
Blob getBlob (String parameterName) throws SQLException;
Clob getClob (String parameterName) throws SQLException;
Array getArray (String parameterName) throws SQLException;
java.sql.Date getDate(String parameterName, Calendar cal)throws SQLException;
java.sql.Time getTime(String parameterName, Calendar cal)throws SQLException;
java.sql.Timestamp getTimestamp(String parameterName, Calendar cal)throws SQLException;
java.net.URL getURL(String parameterName) throws SQLException;
//------------------------- JDBC 4.0 -----------------------------------
RowId getRowId(int parameterIndex) throws SQLException;
RowId getRowId(String parameterName) throws SQLException;
void setRowId(String parameterName, RowId x) throws SQLException;
void setNString(String parameterName, String value)throws SQLException;
void setNCharacterStream(String parameterName, Reader value, long length)throws SQLException;
void setNClob(String parameterName, NClob value) throws SQLException;
void setClob(String parameterName, Reader reader, long length)throws SQLException;
void setBlob(String parameterName, InputStream inputStream, long length)throws SQLException;
void setNClob(String parameterName, Reader reader, long length)throws SQLException;
NClob getNClob (int parameterIndex) throws SQLException;
NClob getNClob (String parameterName) throws SQLException;
void setSQLXML(String parameterName, SQLXML xmlObject) throws SQLException;
SQLXML getSQLXML(int parameterIndex) throws SQLException;
SQLXML getSQLXML(String parameterName) throws SQLException;
String getNString(int parameterIndex) throws SQLException;
String getNString(String parameterName) throws SQLException;
java.io.Reader getNCharacterStream(int parameterIndex) throws SQLException;
java.io.Reader getNCharacterStream(String parameterName) throws SQLException;
java.io.Reader getCharacterStream(int parameterIndex) throws SQLException;
java.io.Reader getCharacterStream(String parameterName) throws SQLException;
void setBlob (String parameterName, Blob x) throws SQLException;
void setClob (String parameterName, Clob x) throws SQLException;
void setAsciiStream(String parameterName, java.io.InputStream x, long length)throws SQLException;
void setBinaryStream(String parameterName, java.io.InputStream x,long length) throws SQLException;
void setCharacterStream(String parameterName,java.io.Reader reader,long length) throws SQLException;
void setAsciiStream(String parameterName, java.io.InputStream x)throws SQLException;
void setBinaryStream(String parameterName, java.io.InputStream x)throws SQLException;
void setCharacterStream(String parameterName,java.io.Reader reader) throws SQLException;
void setNCharacterStream(String parameterName, Reader value) throws SQLException;
void setClob(String parameterName, Reader reader)throws SQLException;
void setBlob(String parameterName, InputStream inputStream)throws SQLException;
void setNClob(String parameterName, Reader reader)throws SQLException;
//------------------------- JDBC 4.1 -----------------------------------
public <T> T getObject(int parameterIndex, Class<T> type) throws SQLException;
public <T> T getObject(String parameterName, Class<T> type) throws SQLException;
//------------------------- JDBC 4.2 -----------------------------------
default void setObject(String parameterName, Object x, SQLType targetSqlType,int scaleOrLength) throws SQLException {
throw new SQLFeatureNotSupportedException("setObject not implemented");
}
default void setObject(String parameterName, Object x, SQLType targetSqlType)throws SQLException {
throw new SQLFeatureNotSupportedException("setObject not implemented");
}
default void registerOutParameter(int parameterIndex, SQLType sqlType)throws SQLException {
throw new SQLFeatureNotSupportedException("registerOutParameter not implemented");
}
default void registerOutParameter(int parameterIndex, SQLType sqlType,int scale) throws SQLException {
throw new SQLFeatureNotSupportedException("registerOutParameter not implemented");
}
default void registerOutParameter (int parameterIndex, SQLType sqlType,String typeName) throws SQLException {
throw new SQLFeatureNotSupportedException("registerOutParameter not implemented");
}
default void registerOutParameter(String parameterName, SQLType sqlType)throws SQLException {
throw new SQLFeatureNotSupportedException("registerOutParameter not implemented");
}
default void registerOutParameter(String parameterName, SQLType sqlType,int scale) throws SQLException {
throw new SQLFeatureNotSupportedException("registerOutParameter not implemented");
}
default void registerOutParameter (String parameterName, SQLType sqlType,String typeName) throws SQLException {
throw new SQLFeatureNotSupportedException("registerOutParameter not implemented");
}
}

继承关系 CallableStatement -> PreparedStatement -> Statement

Connection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public interface Connection  extends Wrapper, AutoCloseable {
Statement createStatement() throws SQLException;
PreparedStatement prepareStatement(String sql)throws SQLException;
CallableStatement prepareCall(String sql) throws SQLException;
String nativeSQL(String sql) throws SQLException;
void setAutoCommit(boolean autoCommit) throws SQLException;
boolean getAutoCommit() throws SQLException;
void commit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
boolean isClosed() throws SQLException;
//======================================================================
// Advanced features:
DatabaseMetaData getMetaData() throws SQLException;
void setReadOnly(boolean readOnly) throws SQLException;
boolean isReadOnly() throws SQLException;
void setCatalog(String catalog) throws SQLException;
String getCatalog() throws SQLException;
int TRANSACTION_NONE = 0;
int TRANSACTION_READ_UNCOMMITTED = 1;
int TRANSACTION_READ_COMMITTED = 2;
int TRANSACTION_REPEATABLE_READ = 4;
int TRANSACTION_SERIALIZABLE = 8;
void setTransactionIsolation(int level) throws SQLException;
int getTransactionIsolation() throws SQLException;
SQLWarning getWarnings() throws SQLException;
void clearWarnings() throws SQLException;
//--------------------------JDBC 2.0-----------------------------
Statement createStatement(int resultSetType, int resultSetConcurrency)throws SQLException;
PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency)throws SQLException;
CallableStatement prepareCall(String sql, int resultSetType,int resultSetConcurrency) throws SQLException;
java.util.Map<String,Class<?>> getTypeMap() throws SQLException;
void setTypeMap(java.util.Map<String,Class<?>> map) throws SQLException;
//--------------------------JDBC 3.0-----------------------------
void setHoldability(int holdability) throws SQLException;
int getHoldability() throws SQLException;
Savepoint setSavepoint() throws SQLException;
Savepoint setSavepoint(String name) throws SQLException;
void rollback(Savepoint savepoint) throws SQLException;
void releaseSavepoint(Savepoint savepoint) throws SQLException;
Statement createStatement(int resultSetType, int resultSetConcurrency,int resultSetHoldability) throws SQLException;
PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException;
CallableStatement prepareCall(String sql, int resultSetType,int resultSetConcurrency,int resultSetHoldability) throws SQLException;
PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)throws SQLException;
PreparedStatement prepareStatement(String sql, int columnIndexes[])throws SQLException;
PreparedStatement prepareStatement(String sql, String columnNames[])throws SQLException;
Clob createClob() throws SQLException;
Blob createBlob() throws SQLException;
NClob createNClob() throws SQLException;
SQLXML createSQLXML() throws SQLException;
boolean isValid(int timeout) throws SQLException;
void setClientInfo(String name, String value)throws SQLClientInfoException;
void setClientInfo(Properties properties)throws SQLClientInfoException;
String getClientInfo(String name)throws SQLException;
Properties getClientInfo()throws SQLException;
Array createArrayOf(String typeName, Object[] elements) throws SQLException;
Struct createStruct(String typeName, Object[] attributes) throws SQLException;
//--------------------------JDBC 4.1 -----------------------------
void setSchema(String schema) throws SQLException;
String getSchema() throws SQLException;
void abort(Executor executor) throws SQLException;
void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException;
int getNetworkTimeout() throws SQLException;
}

ResultSet和PreparedStatement参数绑定的下表 都是从1开始

参考文档

https://www.cnblogs.com/noteless/p/10307273.html