java

public static void main(String[] args) {
		Connection connection = DBConnection.getConn();
		Statement statement = null;
		ResultSet  resultSet = null;
		try {
			statement =connection.createStatement(ResultSet. TYPE_SCROLL_SENSITIVE,
			ResultSet.CONCUR_UPDATABLE);
			String sql = "select name,age,sex from person";
			resultSet = statement.executeQuery(sql);
			System.out.println("修改之前结果集:");
			while(resultSet.next()){
				String name = resultSet.getString("name");
				String age = resultSet.getString("age");
				String sex = resultSet.getString("sex");
				System.out.println("姓名:"+name+" 年龄:"+age+" 性别:"+sex); 
			 }
			resultSet.last();
			//使用updateXXX方法更新列值
			resultSet.updateString("name", "alex");
			resultSet.updateInt("age", 50);
			//提交更新
			resultSet.updateRow();
			//游标移到插入行
			resultSet.moveToInsertRow();
			resultSet.updateString("name","wade");
			resultSet.updateInt("age",28);
			resultSet.updateString("sex","男");
			//提交插入行
			resultSet.insertRow();
			resultSet.close();
			String sql2 = "select name,age,sex from person";
			resultSet = statement.executeQuery(sql2);
			System.out.println("修改之后结果集:");
			while(resultSet.next()){
				String name = resultSet.getString("name");
				String age = resultSet.getString("age");
				String sex = resultSet.getString("sex");
				System.out.println("姓名:"+name+" 年龄:"+age+" 性别:"+sex); 
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, statement, resultSet);
		}
	}
 

你可能感兴趣的:(java,sql)