java基础类库学习 java.sql(2)常用的数据库接口

本文聚焦于Java基础类库中的java.sql包,详细阐述了 JDBC 接口的关键角色,包括Driver接口(数据库驱动)、DriverManager(驱动管理)、Connection(数据库连接)以及Statement和PreparedStatement(SQL语句执行)。PreparedStatement作为Statement的子接口,因其预编译特性而具备更好的性能。ResultSet接口则用于承载执行SQL后返回的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面仔细的学习下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;

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值