日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java spring jdbc_Spring与JDBC支持

發布時間:2023/12/20 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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支持的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。