1. 程式人生 > 實用技巧 >Spring對jdbc的支援學習筆記

Spring對jdbc的支援學習筆記

1. 非加連預執釋方式

2. dataSource對jdbc的支援

  DataSource物件

  ====》JdbcTemplate jdbcTemplate

  ====》String sql = "INSERT INTO product (product_name,sale_price) VALUES (?,?)";

 ====》this.jdbcTemplate.update(sql,product.getProductName(),product.getSalePrice());

3. 一般的連線池使用:
     Connection connection = basicDataSource.getConnection() ;

      String sql="INSERT INTO product (product_name,sale_price) VALUES (?,?)";
      PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,product.getProductName());
preparedStatement.setInt(2,product.getSalePrice());
preparedStatement.executeUpdate();
preparedStatement.close();
4. 原始的jdbc:

// 加
        try {
Class.forName("com.mysql.jdbc.Driver") ;
// 連
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "root");
// 譯
Statement statement = connection.createStatement();
// 執
// String sql = "INSERT INTO product (product_name, sale_price) VALUES ('小來手機', 555)" ;
String sql = "INSERT INTO product (product_name, sale_price) VALUES ('" + productName + "'," + salePrice + ")" ;
statement.executeUpdate(sql) ;
// 釋
statement.close();
connection.close();