国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 竹溪县| 威远县| 武宁县| 吴桥县| 开原市| 武功县| 盐城市| 宝山区| 凌云县| 万年县| 虎林市| 蓝山县| 波密县| 普兰店市| 鱼台县| 永寿县| 年辖:市辖区| 宜章县| 瑞丽市| 兰坪| 陆丰市| 高要市| 柳河县| 安平县| 兴文县| 邵阳市| 宣化县| 汕头市| 会东县| 安多县| 西乡县| 汝州市| 长顺县| 黄陵县| 琼海市| 托克托县| 邵阳县| 同心县| 普兰店市| 周口市| 陈巴尔虎旗|