国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

使用PreparedStatement實現(xiàn)增刪該查

2024-04-27 15:16:45
字體:
供稿:網(wǎng)友

java,servlet中的PReparedStatement 接口繼承了Statement,并與之在兩方面有所不同:有人主張,在JDBC應用中,如果你已經(jīng)是稍有水平開發(fā)者,你就應該始終以PreparedStatement代替Statement.也就是說,在任何時候都不要使用Statement。

以下的代碼段(其中 con 是 Connection 對象)創(chuàng)建包含帶兩個 IN 參數(shù)占位符的 SQL 語句的 PreparedStatement 對象:

PreparedStatement pstmt = con.prepareStatement("UPDATE table4 SET m = ? WHERE x = ?")

在執(zhí)行 PreparedStatement 對象之前,必須設置每個 ? 參數(shù)的值。這可通過調(diào)用 setXXX 方法來完成,其中 XXX 是與該參數(shù)相應的類型。例如,如果參數(shù)具有Java 類型 long,則使用的方法就是 setLong。setXXX 方法的第一個參數(shù)是要設置的參數(shù)的序數(shù)位置,第二個參數(shù)是設置給該參數(shù)的值。例如,以下代碼將第一個參數(shù)設為 123456789,第二個參數(shù)設為 100000000:

pstmt.setLong(1, 123456789);

pstmt.setLong(2, 100000000);

使用PreparedStatement來實現(xiàn)增刪該查的源碼,可反復推敲:

 

package com.bjsxt.bbs2009.service;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashSet;import java.util.Set;import com.bjsxt.bbs2009.model.Category;import com.bjsxt.bbs2009.util.DB;public class CategoryService {	public void add(Category category) {		Connection conn = DB.createConnectionion();		String sql = "insert into _category values (null, ?, ?)";		PreparedStatement ps = DB.prepare(conn, sql);		try {			ps.setString(1, "JavaSE");			ps.setString(2, "JavaSE Description");			ps.executeUpdate();		} catch (SQLException e) {			e.printStackTrace();		}		DB.close(ps);		DB.close(conn);	}		public void update(Category c) {		Connection conn = DB.createConnectionion();		String sql = "update _category set name = ? and descrition = ? where id = ?";		PreparedStatement ps = DB.prepare(conn, sql);		try {			ps.setString(1, c.getName());			ps.setString(2, c.getDescription());			ps.setInt(3, c.getId());			ps.executeUpdate();		} catch (SQLException e) {			e.printStackTrace();		}		DB.close(ps);		DB.close(conn);	}		public void delete(Category c) {		deleteById(c.getId());	}		public void deleteById(int id) {		Connection conn = DB.createConnectionion();		String sql = "delete from _category where id = ?";		PreparedStatement ps = DB.prepare(conn, sql);		try {			ps.setInt(1, id);			ps.executeUpdate();		} catch (SQLException e) {			e.printStackTrace();		}		DB.close(ps);		DB.close(conn);	}		public Set<Category> query() {		Connection conn = DB.createConnectionion();		String sql = "select id, name, description from _category";		PreparedStatement ps = DB.prepare(conn, sql);		ResultSet rs = null;				Set<Category> categories = new HashSet<Category>();		try {			rs = ps.executeQuery();			Category c = null;			while(rs.next()) {				c = new Category();				c.setId(rs.getInt("id"));				c.setName(rs.getString("name"));				c.setDescription(rs.getString("description"));				categories.add(c);			}		} catch (SQLException e) {			e.printStackTrace();		}		DB.close(ps);		DB.close(conn);		return categories;	}}

 

 

 

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 郸城县| 阿图什市| 河北区| 壶关县| 思南县| 桑日县| 罗田县| 滦平县| 托克逊县| 巧家县| 香港| 自治县| 沁水县| 双辽市| 龙南县| 达日县| 隆子县| 出国| 香格里拉县| 剑河县| 峡江县| 莆田市| 嘉黎县| 来安县| 黄冈市| 临夏县| 清涧县| 新巴尔虎右旗| 桑日县| 文登市| 康保县| 西乌| 东兴市| 玉环县| 丰镇市| 新巴尔虎右旗| 乌海市| 航空| 娱乐| 文化| 泸水县|