JDBC的CRUD操作之PreparedStatement的修改操作

        @Test
	/**
	 * 修改操作
	 */
	public void demo2(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		try{
			// 获得连接:
			conn = JDBCUtils.getConnection();
			// 编写SQL语句:
			String sql = "update user set username = ?,password =?,nickname=?,age = ? where id = ?";
			// 预编译SQL:
			pstmt = conn.prepareStatement(sql);
			// 设置参数:
			pstmt.setString(1, "abc");
			pstmt.setString(2, "1234");
			pstmt.setString(3, "旺旺");
			pstmt.setInt(4, 23);
			pstmt.setInt(5, 6);
			// 执行SQL:
			int num = pstmt.executeUpdate();
			if(num > 0){
				System.out.println("修改成功!");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JDBCUtils.release(pstmt, conn);
		}
	}

 

你可能感兴趣的:(MySQL,Java基础)