ajanuskevicius
8/12/2016 - 1:32 PM

Java - JDBC - updateStatement

Java - JDBC - updateStatement

public void updateStatement() throws SQLException {
		
		Connection conn = DB_Util.getConn(DB_Types.MYSQLDB);
		PreparedStatement pstmnt = null;
		
		String sql = "Update City set Population = ? where ID = ?";
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Enter City ID: ");
		int id = scanner.nextInt();
		
		System.out.print("Enter City Population: ");
		int pop = scanner.nextInt();
		
		pstmnt = conn.prepareStatement(sql);
		pstmnt.setInt(1, pop);
		pstmnt.setInt(2, id);
		
		int result = pstmnt.executeUpdate();
		
		if(result == 1) {
			
			System.out.println("Update was successful!");
		}
		else {
			
			System.out.println("Update was not completed!");
		}
		
		scanner.close();
		pstmnt.close();
		conn.close();
	}