jdbc插入一条记录后,马上获取到当前记录的id,以方便对插入的当前记录进行更多的操作

		// save to DB
		Connection conn = null;
		int seqId = 0; //保存序列号1
		String seqfirst = ""; //保存序列号2
		PreparedStatement stmt = null, stmt2 = null, stmt3 = null;
		ResultSet rs = null, rs2 = null;
		String check = "";//判断是否应该插入;

		try {
			conn = conn == null ? JdbcConn.getConn() : conn;
			if (conn.isClosed())
				conn = JdbcConn.getConn();
			// insert record   
			int i = 0, j = 0;

			stmt = conn
					.prepareStatement("INSERT INTO THREEBBFTTHOFFER(id, CRTE_DATE,ORDERDATE,TIMESLOT,FLAT,FLOOR,BLOCK,CONTACT1,CONTACTNO1,CONTACT2,CONTACTNO2,ORDERNUMBER) "
							+ "VALUES (SEQ_3BBFTTH_ID.nextval, SYSDATE, ?,?, ?, ?, ?, ?, ?,?,?,SEQ_3BBBANYANGARDEN_ID.nextval)");

			stmt.setString(++i, registedday);
			stmt.setString(++i, registedtime);
			stmt.setString(++i, room);
			stmt.setString(++i, floor);
			stmt.setString(++i, building);
			stmt.setString(++i, tncAgree1 + name1);
			stmt.setString(++i, phone1);
			stmt.setString(++i, tncAgree2 + name2);
			stmt.setString(++i, phone2);
			stmt.executeUpdate();

			stmt2 = conn
					.prepareStatement("select SEQ_3BBFTTH_ID.CURRVAL from dual");  //获取当前序列的值
			rs = stmt2.executeQuery();
			while (rs.next()) {
				seqId = rs.getInt(1);
			}
			stmt3 = conn
					.prepareStatement("select * from THREEBBFTTHOFFER where id=?");
			stmt3.setInt(++j, seqId);
			rs2 = stmt3.executeQuery();
			while (rs2.next()) {
				dbnumber = rs2.getString(12);
				for (int k = 0; k < 5 - dbnumber.length(); k++) {
					seqfirst += "0";
				}
				dbnumber = seqfirst + dbnumber;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null || rs2 != null) {
				rs.close();
				rs2.close();
			}
			if (stmt != null || stmt2 != null || stmt3 != null) {
				stmt.close();
				stmt2.close();
				stmt3.close();
			}

			if (conn != null) {
				conn.close();
			}
		}
	

你可能感兴趣的:(jdbc插入一条记录后,马上获取到当前记录的id,以方便对插入的当前记录进行更多的操作)