下面仔细的学习下jdbc接口吧
在java.sql包中定义了jdbc接口的各个类
Driver:驱动接口
public interface Driver {
Connection connect(String url, java.util.Properties info)
throws SQLException;
boolean acceptsURL(String url) throws SQLException;
DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info)
throws SQLException;
int getMinorVersion();
}
DriverManager:。用于管理驱动的类
public class DriverManager {
private DriverManager(){}
static {
loadInitialDrivers();
println("JDBC DriverManager initialized");
}
@CallerSensitive获取数据库连接对象
public static Connection getConnection(String url,
String 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()));
}
Connection:数据库连接的接口,用于管理数据库连接的接口,由各大数据库厂商去提供数据库连接的实现
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;
boolean getAutoCommit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
Savepoint setSavepoint(String name) throws SQLException;
Clob createClob() throws SQLException;
void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException;
void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException;
}
Statement:执行sql语句的接口,由各大数据库厂商去提供实现类
public interface Statement extends Wrapper, AutoCloseable {
ResultSet executeQuery(String sql) throws SQLException;
int executeUpdate(String sql) throws SQLException;
void close() throws SQLException;
void setCursorName(String name) throws SQLException;
boolean execute(String sql) throws SQLException;
ResultSet getResultSet() throws SQLException;
int getUpdateCount() throws SQLException;
Connection getConnection() throws SQLException;
}
preparedStatement:预编译的执行sql语句的接口,由各大数据库厂商去提供实现类,是Statement的子接口
允许数据库预编译sql语句,以后每次只改变sql命令的参数,避免数据库每次都需要编译sql语句,因此性能更好
因此使用preparredStatement执行sql时,无需在传入sql语句,只要为预编译的sql语句传入参数即可
public interface PreparedStatement extends Statement {
ResultSet executeQuery() throws SQLException;
int executeUpdate() throws SQLException;
void setTimestamp(int parameterIndex, java.sql.Timestamp x)
throws SQLException;
void clearParameters() throws SQLException;
boolean execute() throws SQLException;
void setArray (int parameterIndex, Array x) 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 setNString(int parameterIndex, String value) throws SQLException;
void setClob(int parameterIndex, Reader reader)
throws SQLException;
}
ResultSet结果集类的接口,由各大数据库厂商去提供实现
public interface ResultSet extends Wrapper, AutoCloseable {
boolean next() throws SQLException;
void close() throws SQLException;
String getString(int columnIndex) throws SQLException;
boolean getBoolean(int columnIndex) throws SQLException;
java.io.InputStream getBinaryStream(int columnIndex)
throws SQLException;
Blob getBlob(String columnLabel) throws SQLException;
}