ResultSet.getStatement 是用来获得ResultSet的生产者,如果是由Statement产生则返回Statement对像,如果结果集合对像是由 DatabaseMetaData产生的,那么这个方法应该返回null.
针对说明做一次代码测试(java8, tomcat-jdbc-9.0.38.jar,mariadb-java-client-2.6.0.jar)
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import java.sql.*;
public class TestResultSetGetStatement {
static String driver = "org.mariadb.jdbc.Driver";
static String url = "jdbc:mariadb://localhost/test";
static String user = "root";
static String password = "";
public static void main(String[] args) throws Exception {
testMysqlDriver();
testTomcatJdbc();
}
private static void testMysqlDriver() throws Exception {
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, password);
test("MysqlDriver", con);
}
private static void testTomcatJdbc() throws Exception {
PoolProperties p = new PoolProperties();
p.setUrl(url);
p.setDriverClassName(driver);
p.setUsername(user);
p.setPassword(password);
DataSource ds = new DataSource();
ds.setPoolProperties(p);
try {
test("TomcatJdbc", ds.getConnection());
} finally {
if (ds != null) ds.close();
}
}
public static void test(String sourceName, Connection con) throws SQLException {
Statement st = null;
ResultSet re1 = null;
ResultSet re2 = null;
try {
st = con.createStatement();
re1 = st.executeQuery("select 1 from dual");
if (re1.getStatement() != st) {
System.out.println("<" + sourceName + ">ResultSet.getStatement!=owner Statement");
}
DatabaseMetaData dataMeta = con.getMetaData();
if (dataMeta.getConnection() != con) {
System.out.println("<" + sourceName + ">DatabaseMetaData.getConnection!=owner connection");
}
re2 = dataMeta.getCatalogs();
if (re2.getStatement() != null)
System.out.println("<" + sourceName + ">ResultSet(from DatabaseMetaData).getStatement!=null");
} finally {
if (re1 != null) re1.close();
if (re2 != null) re2.close();
if (st != null) st.close();
if (con != null) con.close();
}
}
}
结果截图