一、JDBC驱动加载和注册
Class.forName("org.hsqldb.jdbcDriver");
这行代码是用于加载一个JDBC驱动并注册这个已经加载的驱动,可是我们并没有看到加载和注册的代码片段,这行代码的背后隐藏着些什么呢?先来看看org.hsqldb.jdbcDriver这个类的java源代码:
public class jdbcDriver
implements Driver
{
public jdbcDriver()
{
}
public Connection connect(String s, Properties properties)
throws SQLException
{
return getConnection(s, properties);
}
public static Connection getConnection(String s, Properties properties)
throws SQLException
{
HsqlProperties hsqlproperties = DatabaseURL.parseURL(s, true);
if (hsqlproperties == null)
throw new SQLException(Trace.getMessage(62));
if (hsqlproperties.isEmpty())
{
return null;
} else
{
hsqlproperties.addProperties(properties);
return new jdbcConnection(hsqlproperties);
}
}
public boolean acceptsURL(String s)
{
return s != null && s.regionMatches(true, 0, "jdbc:hsqldb:", 0, "jdbc:hsqldb:".length());
}
public DriverPropertyInfo[] getPropertyInfo(String s, Properties properties)
{
String as[] = {
"true", "false"
};
DriverPropertyInfo adriverpropertyinfo[] = new DriverPropertyInfo[6];
DriverPropertyInfo driverpropertyinfo = new DriverPropertyInfo("user", null);
driverpropertyinfo.value = properties.getProperty("user");
driverpropertyinfo.required = true;
adriverpropertyinfo[0] = driverpropertyinfo;
driverpropertyinfo = new DriverPropertyInfo("password", null);
driverpropertyinfo.value = properties.getProperty("password");
driverpropertyinfo.required = true;
adriverpropertyinfo[1] = driverpropertyinfo;
driverpropertyinfo = new DriverPropertyInfo("get_column_name", null);
driverpropertyinfo.value = properties.getProperty("get_column_name", "true");
driverpropertyinfo.required = false;
driverpropertyinfo.choices = as;
adriverpropertyinfo[2] = driverpropertyinfo;
driverpropertyinfo = new DriverPropertyInfo("ifexists", null);
driverpropertyinfo.value = properties.getProperty("ifexists");
driverpropertyinfo.required = false;
driverpropertyinfo.choices = as;
adriverpropertyinfo[3] = driverpropertyinfo;
driverpropertyinfo = new DriverPropertyInfo("default_schema", null);
driverpropertyinfo.value = properties.getProperty("default_schema");
driverpropertyinfo.required = false;
driverpropertyinfo.choices = as;
adriverpropertyinfo[4] = driverpropertyinfo;
driverpropertyinfo = new DriverPropertyInfo("shutdown", null);
driverpropertyinfo.value = properties.getProperty("shutdown");
driverpropertyinfo.required = false;
driverpropertyinfo.choices = as;
adriverpropertyinfo[5] = driverpropertyinfo;
return adriverpropertyinfo;
}
public int getMajorVersion()
{
return 1;
}
public int getMinorVersion()
{
return 8;
}
public boolean jdbcCompliant()
{
return false;
}
static
{
try
{
DriverManager.registerDriver(new jdbcDriver());
}
catch (Exception exception) { }
}
}
注意到最后的代码片段是一个被static包含的静态语句块:
static
{
try
{
DriverManager.registerDriver(new jdbcDriver());
}
catch (Exception exception) { }
}
java的运行机制,静态语句块是在类的class文件首次被装载到JVM中时马上就要执行并且只执行一次的代码,所以当我们使用Class.forName("org.hsqldb.jdbcDriver");把的class文件装载到JVM时,这行代码背后就通过上面的静态语句块已经完成了JDBC驱动的加载实例化并使用DriverManager类注册这个驱动的操作了
二、statement、preparedStatement
Statement是用于向数据库发送SQL语句、执行SQL语句并返回结果。
当你需要多次使用一个Statement对象时,使用PreparedStatement对象更有效率。PreparedStatement对象和Statement对象不同的是,PreparedStatement对象在实例化创建时就被设置了一个SQL语句,使用PreparedStatement对象执行的SQL语句在首次发送到数据库时,SQL语句会被编译,所以PreparedStatement对象是一个包含了已经预编译SQL语句的对象,这样当多次执行同一个SQL语句时,就不用每次都去编译SQL语句,而是直接执行已经编译过的SQL语句。
经过代码测试:
Connection conn = JdbcUtil.getDefaultConnection();
Statement stmt = JdbcUtil.getDefaultConnection().createStatement();
int executeTime = 1000000;
long start = System.currentTimeMillis();
for (int i = 0; i < executeTime; i++) {
stmt.executeQuery("select * from coffees");
}
long end = System.currentTimeMillis();
System.out.println(end - start);
PreparedStatement preStmt = conn
.prepareStatement("select * from coffees");
long preStart = System.currentTimeMillis();
for (int i = 0; i < executeTime; i++) {
preStmt.executeQuery();
}
long preEnd = System.currentTimeMillis();
System.out.println(preEnd - preStart);
assertTrue((end - start) > (preEnd - preStart));
// 关闭jdbc相关对象
stmt.close();
preStmt.close();
conn.close();
使用Statement对象和PreparedStatement对象执行同一个SQL语句1000000次所消耗的时间分别为:
Statement:112406
PreparedStatement:85953
可以看出PrepareStatment执行SQL语句的效率确实会提高。所以在日常开发中,我们是优先使用preparedstatement的
分享到:
相关推荐
Statement和PreparedStatement之间的区别
sqlserver各版本驱动 DriverManager:负责加载各种不同驱动程序(Driver),并根据不同的请求,向调用者返回...PreparedStatement:用以执行包含动态参数的SQL查询和更新(在服务器端编译,允许重复执行以提高效率)
1 将数据库的JDBC驱动加载到classpath中,在基于JAVAEE的WEB应用实际开发过程中,通常要把目标数据库产品的JDBC驱动复制到WEB-INF/lib下. 2 加载JDBC驱动,并将其注册到DriverManager中。 3 建立数据库连接,取得...
jdbc中preparedStatement比Statement的好处
Java面试题34.jdbc中preparedStatement比Statement的好处.mp4
java中PreparedStatement和Statement的区别
主要介绍了详解Java的JDBC中Statement与PreparedStatement对象,PreparedStatement一般来说比使用Statement效率更高,需要的朋友可以参考下
JDBC基础教程之PreparedStatement.doc )
JDBC JDBC(Java Database Connectivity)可以为多种关系型数据库提供统一的访问操作接口。...要先加载数据库驱动: 装载MySql驱动:Class.forName(“com.mysql.jdbc.Driver”); 装载Oracle驱动:Class.for
2.加载驱动类 3.创建数据库连接对象`Connection` 4.创建`Statement` (此处存在SQL注入问题) 5.执行SQL 6.解析查询对象`ResultSet` 7.关闭连接 `ResultSet 、Statement 、Connection` 3.SQL注入问题与解决(Statement...
NULL 博文链接:https://singleant.iteye.com/blog/1298837
jdk1.8可使用的jdbc驱动... DriverManager:负责加载各种不同驱动程序(Driver),并根据不同的请求,向调用者返回相应的数据库连接(Connection)。Driver:驱动程序,会将自身加载到DriverManager中去,并处理...
1、JDBC(Java Database Connection):java连接数据库统一接口API,底层主要通过直接的JDBC驱动和 JDBC-ODBC桥驱动实现与数据库的连接。 1>.JDBC驱动程序类型: <1>.JDBC-ODBC桥加ODBC驱动程序:需要ODBC驱动,适合...
JDBC中PreparedStatement接口提供的execute、executeQuery和executeUpdate之间的区别及用法
Oracle的PreparedStatement类不完全支持BLOB和CLOB等大对象的处理,尤其是Thin驱动程序不支持利用PreparedStatement对象的setObject()和setBinaryStream()方法设置BLOB的值,也不支持利用setCharacterStream()方法...
作为初始化的一部分,此接口会尝试加载在”jdbc.drivers”系统属性中引用的驱动程序。只是一个辅助类,是工具。 java.sql.Statement 用于执行静态SQL语句并返回其生成结果的对象。 java.sql.PreparedStatement ...
JDBC简介 JDBC驱动程序类型 JDBC应用程序的结构 Statement接口 PreparedStatement接口 知识点详细生动具体
本章将详细介绍数据库操作,如连接数据库、添加记录、... 熟练掌握加载JDBC驱动程序 熟练掌握连接MySQL数据库 熟练掌握添加、更新、删除记录 熟练掌握获得指定记录 熟练掌握使用PreparedStatement进行数据库操作
jdbc2.0版 PreparedStatement接口的用法
JDBC主要接口、类: Connection:封装连接 DriverManager:管理驱动 Statement:封装SQL语句 PreparedStatement:封装SQL语句 ResultSet:封装结果集