java操作数据库常用例子

select

public List<WorklsBean> getHXdatas(String recordId){
		List<WorklsBean> wlist = new ArrayList<WorklsBean>();
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "";
		try {
			sql = "select * from whir$sgzzb where whir$sgzzb_foreignkey='"+recordId+"' order by whir$sgzzb_xh";
			conn = db.getConn();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				WorklsBean w = new WorklsBean();
				w.setGh(rs.getString("whir$sgzzb_gh"));
				w.setXm(rs.getString("whir$sgzzb_xm"));
				w.setBm(rs.getString("whir$sgzzb_bm"));
				wlist.add(w);
			}
			System.out.println("sql:"+sql);
		} catch (SQLException e) {
			System.out.println("error sql:"+sql);
			e.printStackTrace();
		} finally {
			db.close(conn);
		}
		return wlist;
	
	}

insert

public boolean saveAddress(ReceiveAddress reAddress) throws Exception {
		DBConnection db = new DBConnection();	
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "insert into d_d_receive_address(user_id,receive_name,full_address,postal_code,mobile,phone,is_default)values(?,?,?,?,?,?,?)";
			con = db.getConn();
			stmt = con.prepareStatement(sql);
			stmt.setInt(1, reAddress.getUser_id());
			stmt.setString(2, reAddress.getReceive_name());
			stmt.setString(3, reAddress.getFull_address());
			stmt.setString(4, reAddress.getPostal_code());
			stmt.setString(5, reAddress.getMobile());
			stmt.setString(6, reAddress.getPhone());
			stmt.setInt(7, 1);
			int result = stmt.executeUpdate();
			if(result==0){
				return false;
			}
		} finally {
			db.close(rs, stmt, con);
		}
		return true;
	}

批量inser

public void insertItem(List<WorklsBean> list,int foreignkey) {
		DBConnection db = new DBConnection();
		Connection conn = db.getConn();
		try {
			conn.setAutoCommit(false);
			PreparedStatement pst = conn.prepareStatement("insert into whir$sgzzb(" +
					"whir$sgzzb_id,whir$sgzzb_gh,whir$sgzzb_foreignkey)" +
					"values(hibernate_sequence.nextval,?,?,"+foreignkey+")");
			int index =0;
			for (int i = 0; i < list.size(); i++) {
				index = i+1;
				WorklsBean w = list.get(i);
				pst.setString(1, w.getGh();
				pst.addBatch();
				if (i % 1000 == 0 || i == (list.size() - 1)) {// 可以设置不同的大小;如50,100,500,1000等等
					pst.executeBatch();
					conn.commit();
					pst.clearBatch();
				}
			}
			pst.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn);
		}
	}

delete

	public void deletMasterData() {
		Connection conn = null;
		Statement stmt = null;
		String sql = "";
		//删除数据
		try {
			conn = db.getConn();
			stmt = conn.createStatement();
			sql = "delete from whir$sgz where whir$sgz_drbj='1'";
			stmt.execute(sql);
			System.out.println("delete sql:"+sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.close(stmt, conn);
		}
	}

update

public void updateJYBJforOne(String id,String userName,String time){
		Connection conn = db.getConn();
		Statement stmt = null;
		String updateSQL= "";
		try {
			stmt = conn.createStatement();
			updateSQL = "update whir$khgl set whir$khgl_sfjy='1',whir$khgl_jyr='"+userName+"',whir$khgl_jysj='"+time+"' where whir$khgl_id="+id;
			stmt.execute(updateSQL);
			System.out.println("updateSQLsql:"+updateSQL);
		} catch (SQLException e) {
			System.out.println("error sql:"+updateSQL);
			e.printStackTrace();
		} finally {
			db.close(stmt, conn);
		}
	}

批量update

public void updateJforAll(String[] ids,String userName,String time){
		DBConnection db = new DBConnection();	
		Connection conn = db.getConn();
		try {
			conn.setAutoCommit(false);
			PreparedStatement pst = conn.prepareStatement("update whir$khgl set whir$khgl_sfjy='1',whir$khgl_jyr='"+userName+"'," +
					"whir$khgl_jysj='"+time+"' where whir$khgl_id=?");
			for(int i=0;i<ids.length;i++){
				pst.setString(1,ids[i]);
				pst.addBatch();
				if (i % 1000 == 0 || i == (ids.length - 1)) {// 可以设置不同的大小;如50,100,500,1000等等
					pst.executeBatch();
					conn.commit();
					pst.clearBatch();
				}
			}
			pst.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn);
		}	
	}




 

使 用 说 明 该光盘中有《Java数据库编程实例》一书中的所有实例源程序代码,编译通过的实例类代码 和JBuilder工程文件,以及实例中应用的数据库,并有在实例中涉及的相关Java类文件(数据库 驱动程序)。配书光盘中全部内容包括: 1. 各章实例源程序 实例源程序以及实例中应用的数据库在光盘的各章实例目录中,使用各章实例程序并需要重新 编译时,请将源程序复制到硬盘,然后修改所有复制文件的属性,删除所有文件的只读属性, 运行时需要在数据库管理系统中注册数据库,如果是服务器端程序需要启动服务器系统。 所有实例都是在JBuilder开发环境中进行开发和编译的。 2. JBuilder集成开发环境 在Borland公司的网站上www.borland.com/jbuilder有为用户免费提供的Jbuilder4~Jbuilder8 集成开发环境,在下载Jbuilder集成开发环境之前,首先需要注册成为该网站的注册会员, Borland公司为下载者提供注册会员的Serial number和Key,然后进入Borland公司网站上的免费 下载区下载Jbuilder集成开发环境。 安装Jbuilder集成开发环境时,首先使用WINZIP将压缩的Jbuilder.zip文件解压缩到一个临时目录 中,然后执行临时目录中的setup.exe可执行文件,按照JBuilder安装向导完成JBuilder开发环境的 安装。在启动JBuilder开发环境后,当需要Serial number和Key时请查看该软件提供的Serial number 和Key。 在使用该光盘有技术问题时,请发送电子邮件到:sunyilin@elec.bnu.edu.cn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值