javascript
java spring jdbc_Spring与JDBC支持
JDBC是一種標準Java編程接口(JAVA API),可以將Java編程語言與廣泛數據庫進行連接。
JDBC API庫包含下面提到的每個任務,都是與數據庫相關的常用用法。數據庫的連接
創建sql語句
執行或提交sql語句
查看或修改查詢到的記錄
從根本上說,JDBC是一種規范,它提供了一套完整的接口,可以便攜式訪問底層數據庫。
JDBC架構
JDBC API支持兩層或三層的處理模式連接數據庫,但一般只使用兩層處理模式。JDBC API:提供了應用程序對JDBC管理器的連接。
JDBC DRIVER API:提供了JDBC管理器對驅動程序的連接。
JDBC API使用驅動程序管理器和數據庫特定的驅動程序來提供異構(heterogeneous)數據庫的透明連接。
JDBC驅動程序管理器可確保正確的驅動程序訪問每個數據源,驅動程序管理器能夠支持連接多個異構數據庫的多個并發驅動程序。
JDBC API 提供了以下接口和類
DriverManager:這個類管理一系列數據庫驅動程序。使用通信子協議從Java應用程序中請求合適的數據庫驅動程序,建立數據庫連接。
Driver:這個接口處理與數據庫服務器的通信。我們將很少直接與驅動陳旭互動。
Connection:此接口具有操作數據庫的所有方法。改連接對象表示通信上下文,即所有與數據庫的通信僅通過這個對象連接。
Statement:這個接口的對象將sql語句提交到數據庫。
ResultSet:這個接口保存sql語句查詢結果。
SqlException:這個類處理發生在數據庫應用程序的任何錯誤。
代碼示例
Customer類
public class Customer {
int custId;
String name;
int age;
//getter and setter methods}
數據庫訪問DAO
public interface CustomerDAO{
public void insert(Customer customer);
public Customer findByCustomerId(int custId);
}
/*** @author :fengzhiwei* @date :Created in 2019/6/10 11:16* @description:${description}*/
public class JdbcCustomerDAO implements CustomerDAO {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void insert(Customer customer) {
StringBuilder sb = new StringBuilder();
sb.append("insert into customer ")
.append("(cust_id, name, age) ")
.append("value ")
.append("(?,?,?)");
Connection connection = null;
try {
connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sb.toString());
preparedStatement.setInt(1, customer.getCustId());
preparedStatement.setString(2, customer.getName());
preparedStatement.setInt(3, customer.getAge());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public Customer findByCustomerId(int custId) {
StringBuilder sb = new StringBuilder("select * from cutomer where cust_id = ?");
Connection connection = null;
Customer customer = null;
try {
connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sb.toString());
preparedStatement.setInt(1, custId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
customer = new Customer();
customer.setCustId(resultSet.getInt("cust_id"));
customer.setAge(resultSet.getInt("age"));
customer.setName(resultSet.getString("name"));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return customer;
}
}
Spring 配置文件
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
總結
以上是生活随笔為你收集整理的java spring jdbc_Spring与JDBC支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云公司简介
- 下一篇: 详细过程!SpreadJS助力企业轻松构